Function design using MATLAB
1- To Compute the foumla (2*k^2 - k) for each elements of the line 1-10 step
+2:
function func1()
total=0;
for k = [Link]
total = total + 2*k^2 - k;
disp(total)
end
end
-------------------
func1()
2- To compute the result of general second-order equationbased on the value of
delta:
function vals(a, b, c)
delta = b^2-4*a*c
if delta>0
x1=(-b+sqrt(delta))/(2*a)
x2=(-b-sqrt(delta))/(2*a)
elseif delta<0
disp('the roots are complex')
else
x1_2=(-b/(2*a))
end
-----------------
clear all, clc;
a=input('a=');
b=input('b=');
c=input('c=');
vals(a, b, c)
3- This function calculates the maximum of the,five numbers given as input:
Function design using MATLAB
function max = mymax(n1, n2, n3, n4, n5)
%This function calculates the maximum of the
% five numbers given as input
max = n1;
if(n2 > max)
max = n2;
end
if(n3 > max)
max = n3;
end
if(n4 > max)
max = n4;
end
if(n5 > max)
max = n5;
end
end
-----------------
clear all, clc;
n1=input('n1=');
n2=input('n2=');
n3=input('n3=');
n4=input('n4=');
n5=input('n5=');
do=mymax(n1, n2, n3, n4, n5);
fprintf('The max value = %d\n',do)
4- To find the Pythagorean theorem
function [ c ] = pytha( a,b )
c = sqrt(a^2+b^2);
end
------------------
clear all, clc;
a=input('a = ');
b=input('b = ');
Function design using MATLAB
do=pytha( a,b );
fprintf('The 4result = %.2f\n',do)