0% found this document useful (0 votes)
34 views10 pages

Sheet 4: Eng. Mohammed Elsayed

This document contains MATLAB code examples and exercises: 1) A program to sort a 2-element vector in ascending order using a swap function. 2) A function that returns the maximum of 3 inputs without using built-in functions. 3) A rewrite of the previous program using if-elseif-else. 4) A program to calculate the mean of a vector of any length. 5) A question about finding the value of an array in a MATLAB program. 6) A function that determines a number given its factorial as input.

Uploaded by

ahmed fares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

Sheet 4: Eng. Mohammed Elsayed

This document contains MATLAB code examples and exercises: 1) A program to sort a 2-element vector in ascending order using a swap function. 2) A function that returns the maximum of 3 inputs without using built-in functions. 3) A rewrite of the previous program using if-elseif-else. 4) A program to calculate the mean of a vector of any length. 5) A question about finding the value of an array in a MATLAB program. 6) A function that determines a number given its factorial as input.

Uploaded by

ahmed fares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SHEET 4

Eng. Mohammed Elsayed


Ex1: A vector s contains only two elements. Write a MATLAB program to
sort the vector s into ascending order. Use the swap() function in your code.

%clear;
%s = [2, 1];
%if (s(1) > s(2))
%[s(1), s(2)] = swap(s(1), s(2));
%end

function [ a, b ] = swap( x , y )
temp = x;
a = y;
b = temp;
end
• Ex2: Write a MATLAB function that has three input arguments and returns
the maximum value of its input arguments. Name this function max3a. This
function should not call any other functions.

%max= max3a( 20, 18, 23)


function maximum = max3a( a, b, c )
if(a>=b && a>=c)
maximum=a;
elseif(b>a && b>c)
maximum=b;
else
maximum=c;
end
end
• Ex3: Rewrite the program using an if-elseif-else statement:
Sol.:
Ex4: Write a MATLAB program to calculate the mean of a vector. Your
program should be able to calculate the mean of the vector irrespective of its
length. The mean of a vector is given by the following equation:

where v(1) is the first element of the vector v and n is the number of elements
in the vector.
Sol.:
Ex5: Find the value of the array A in the MATLAB program:
Ex6: Write a MATLAB function that determines a number if its factorial
is given. The function has one input argument and returns one value. For
example, if the number 120 is given to the function, it returns the value 5.
Give this function a meaningful and descriptive name.
function number = No_for_given_factorial(
factorial )
number=1;
while(true)
factorial=factorial/(number+1);
if(factorial<1)
break;
end function number = No_for_given_factorial(
number=number+1; factorial )
end number=1;
end counter=1;
result=1;
while(true)
result=result*counter;
if(result==factorial)
break;
end
number=number+1;
counter=counter+1;
end
end

You might also like