0% found this document useful (0 votes)
13 views3 pages

MATLAB Functions for Beginners

This document contains 4 examples of MATLAB functions: 1) A function to calculate the formula (2*k^2 - k) from 1 to 10. 2) A function to calculate the roots of a quadratic equation based on the discriminant. 3) A function to find the maximum of 5 input numbers. 4) A function to calculate the hypotenuse using the Pythagorean theorem.

Uploaded by

as739562978
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)
13 views3 pages

MATLAB Functions for Beginners

This document contains 4 examples of MATLAB functions: 1) A function to calculate the formula (2*k^2 - k) from 1 to 10. 2) A function to calculate the roots of a quadratic equation based on the discriminant. 3) A function to find the maximum of 5 input numbers. 4) A function to calculate the hypotenuse using the Pythagorean theorem.

Uploaded by

as739562978
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

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)

You might also like