Sheet 4: Eng. Mohammed Elsayed
Sheet 4: Eng. Mohammed Elsayed
%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.
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