How to Implement a Custom Max Function in MATLAB?

In summary, this conversation discusses creating a user-written function in MATLAB to find the maximum value in a vector of values. The function should use a FOR loop and an IF statement and be tested with different input sets. It is recommended to read the MATLAB documentation on working with M-files and understanding the difference between scripts and functions before attempting this task.
  • #1
BMY61
3
0

Homework Statement



Consider how you would pick the maximum value from a list (vector) of values. You take the 1st value in the vector and compare the next value to it. If the next value is larger, then select the next value as the max. You would continue like that moving on to the next value, next value, etc, until the end of vector is reached.

Write a user-written-function in MatLab (name that function as MyMax) to select maximum value from a vector which is given to MyMax as its input argument. Use a FOR loop and an IF statement inside the loop. Please test it by running it on MatLab for several different input value sets. Give the printout of the program to the instructor. Do NOT use any other MatLab function, except length, size, fprintf, and input

To test it, use the following:
A = input (‘ give 10 integer values ‘);
maxA = MyMax(A);
fprintf(‘Max val = %f\n’, maxA);



Homework Equations





The Attempt at a Solution



i have no clue how to create a user written function for this problem, does anyone have any ideas?
 
Physics news on Phys.org
  • #2
I'd highly recommend you start by reading through the following page on the Mathworks MATLAB documentation site ("Working with M-Files"):
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-41453.html

Once you've got that down, go to the next page over, "M-File Scripts and Functions" to figure out the distinction between scripts (code blocks that help you avoid typing pages of MATLAB commands, may or may not assume that you've already done a bunch of stuff) and functions (code blocks that you run with specific inputs and outputs):
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html

You probably want a function for this task, but don't you have a lab or seminar (or even class time, if you happen to be an engineering student) to cover MATLAB basics?
 
Last edited by a moderator:
  • #3


it is important to understand and be proficient in programming languages such as MATLAB. In this case, the task is to create a user-written function to find the maximum value from a given vector using a FOR loop and an IF statement. Here is a possible solution to the problem:

% Define the function MyMax with input argument vector A
function max_val = MyMax(A)

% Initialize the maximum value as the first element of the vector
max_val = A(1);

% Use a FOR loop to compare each value in the vector to the current maximum
for i = 1:length(A)
% Use an IF statement to check if the current value is larger than the maximum
if A(i) > max_val
% If it is, update the maximum value
max_val = A(i);
end
end

% Print the maximum value
fprintf('Max val = %f\n', max_val);

% Test the function with different input value sets
A = input('Give 10 integer values: ');
maxA = MyMax(A); % Call the function with input argument A
fprintf('Max val = %f\n', maxA); % Print the maximum value

% Repeat the process with different input value sets
B = input('Give 5 integer values: ');
maxB = MyMax(B);
fprintf('Max val = %f\n', maxB);

C = input('Give 3 integer values: ');
maxC = MyMax(C);
fprintf('Max val = %f\n', maxC);

% Sample output:
% Give 10 integer values: [5 2 8 10 3 6 9 1 4 7]
% Max val = 10.000000
% Give 5 integer values: [2 4 6 8 10]
% Max val = 10.000000
% Give 3 integer values: [7 3 9]
% Max val = 9.000000

This user-written function uses a simple algorithm to compare each value in the vector to the current maximum and update it if a larger value is found. It can be tested with different input value sets to demonstrate its functionality. By utilizing only basic MATLAB functions, it also ensures that the solution is not dependent on any built-in functions and can be easily understood and modified by others.
 
1.

What is a user written function in MATLAB?

A user written function in MATLAB is a custom function created by a user to perform a specific task or set of tasks. It allows for more efficient and organized coding by breaking down complex tasks into smaller, more manageable functions.

2.

How do I create a user written function in MATLAB?

To create a user written function in MATLAB, you can use the "function" keyword followed by the name of the function and input and output variables. The code for the function will then be written in between the "function" line and the "end" line.

3.

What is the purpose of passing input variables to a user written function?

Passing input variables to a user written function allows for the function to perform specific tasks based on the given input. This allows for more flexibility and reusability of the function in different scenarios.

4.

Can a user written function have multiple output variables in MATLAB?

Yes, a user written function can have multiple output variables in MATLAB. These output variables can be specified in the function declaration and their values can be assigned using the "return" keyword within the function.

5.

How can I use the user written function in my MATLAB code?

To use a user written function in MATLAB, you can call the function by its name and pass in the necessary input variables. The function will then execute and return the output variables, which can be stored in variables or used directly in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
810
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top