Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The MATLAB question: You should now be familiar with MATLAB\'s built-in \"max\"

ID: 3704395 • Letter: T

Question

The MATLAB question:

You should now be familiar with MATLAB's built-in "max" function. Without using the built-in max(), min() and sort() functions, write a function that takes vector as an input and returns the maximum value as well as the index at which the maximum value occurs. Your function should work with input vectors of any length. Your function header should be similar to the following: function [max_value, index] mymax(x) Test your function for fxvalues between O and 50 with a step size of 0.001. Check the maximum value and index using MATLAB's built-in max() function. Plot f(x) and mark the maximum point. Remember to include a legend. Although not necessary for this task, think about how you would code this for a two-dimensional matrix by creating a function which finds the maximum value and its corresponding row and column. function [max-value, row, column] = mymax2(x)

Explanation / Answer

ScreenShot

Function to find maximum and index of a vector

%function to get maximum value and index of maximum value of a vector
function[max_value,index]=mymax(x)
%set maximum value as zero
   max_value=0;
   %find out the size of vector
   [n,m]=size(x);
   %loop through until the size
   for i=1:m
   %set max_value and index
    if max_value<x(i)
       max_value=x(i);
       index=i;
    end
   end
end

---------------------------------------------------------------

Test function

%for loop to calculate f(x)=x^(1/x)
for i=.001:.001:49.999
x=i^(1/i);%generate x vector
end
%find max and index using buit in function
[val,idx]=max(x);
fprintf('The value of max and index using in built function %d and %d ',val,idx)
%function to get maximum value and index of maximum value of a vector
function[max_value,index]=mymax(x)
%set maximum value as zero
   max_value=0;
   %find out the size of vector
   [n,m]=size(x);
   %loop through until the size
   for i=1:m
   %set max_value and index
    if max_value<x(i)
       max_value=x(i);
       index=i;
    end
   end
end
%find max and index using user defined function
[a,b]=mymax(x);
fprintf('The value of max and index using user defined function,max_value=%d and index=%d',a,b)

----------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote