0% found this document useful (0 votes)
7 views

Functions Examples

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)
7 views

Functions Examples

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
You are on page 1/ 4

The General function builder command

function [ output_args ] = untitled9( input_args )


%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here

end

1. Create a function to Compute cumulative of the formula (2*k^2 - k) for each


element of the line (1-10) step +2:
Function

function [me] =func1()


%Create a function to Compute the foumla (2*k^2 - k)
for each elements of
%the line (1-10) step +2:
total=0;
for k = 2:2:10
op = 2*k^2 - k;
total = total + op;
me=total;
fprintf('The result = %d\n',me)
end
end

Scripting

m=func1();

2. Create a function to compute the result of general second-order equation


based on the value of delta:

Function

function[x]= vals(a, b, c)
%Create a function to compute the result of general
second-order equation

1
%based on the value of delta:
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

Scripting

clear all, clc;


a=input('a=');
b=input('b=');
c=input('c=');
vals(a, b, c)

3. Create a function to calculates the maximum of the five numbers given as


input:
Function

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

2
Scripting

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. Create a function to find the Pythagorean theorem


Function

function [ c ] = pytha1( a,b )


%To find the Pythagorean theorem
c=sqrt(a^2 + b^2);
end

Scripting

clear all, clc;


a=input('a = ');
b=input('b = ');
do=pytha( a,b );
fprintf('The 4result = %.2f\n',do)

3
5. Create a function to Compute the cubic for each elements of the start loop and
end which entered by user and get the total of all cubic?

Function

function [ l ] = cubicme( s, e )
%Create a function to Compute the cubic for each
elements of the stsrt
%loop and end which entered by user and get the total
of all cubic?
total=0;
for i = s:e
l=i^3;
total=total+l;
fprintf('The cubic of(%d)= %d\n',i,l)
end
fprintf('\nThe total of all cubics = %d\n',total)

Scripting

clear all, clc;


fprintf('Enter the start and end loop\n')
fprintf('-----------------------------\n\n')
n1=input('Enter the start loop: ');
n2=input('Enter the start loop: ');
fprintf('\n')
if (n1>n2)
fprintf('The start loop should be less than end
loop!\n')
fprintf('Run the script again\n')
else
me=cubicme(n1,n2);
end

You might also like