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

MATLAB Will Execute The Above Statement and Return The Following Result

The document describes MATLAB functions for calculating the maximum of a set of numbers, finding the roots of a quadratic equation, and calculating the discriminant of a quadratic equation. It includes code for functions to calculate the maximum, find quadratic roots directly and using a nested function, and defines a discriminant sub-function. Examples are provided to demonstrate executing the functions and returning results.

Uploaded by

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

MATLAB Will Execute The Above Statement and Return The Following Result

The document describes MATLAB functions for calculating the maximum of a set of numbers, finding the roots of a quadratic equation, and calculating the discriminant of a quadratic equation. It includes code for functions to calculate the maximum, find quadratic roots directly and using a nested function, and defines a discriminant sub-function. Examples are provided to demonstrate executing the functions and returning results.

Uploaded by

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

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
mymax(34, 78, 89, 23, 11)

MATLAB will execute the above statement and return the following
result
ans = 89

function [x1,x2] = quadratic(a,b,c)


%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficients of x2, x and the
%constant term
% It returns the roots
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);

end % end of quadratic

function dis = disc(a,b,c)


%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function

quadratic(2,4,-4)

MATLAB will execute the above statement and return the following
result
ans = 0.7321

function [x1,x2] = quadratic2(a,b,c)


function disc % nested function
d = sqrt(b^2 - 4*a*c);
end % end of function disc
disc;
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of function quadratic2

function dis = disc(a,b,c)


%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function

Create a function quadratic3.m in your working directory and type the


following code in it

function [x1,x2] = quadratic3(a,b,c)


%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficient of x2, x and the
%constant term
% It returns the roots
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of quadratic3

Format

Purpose

A(:,j)

is the jth column of A.

A(i,:)

is the ith row of A.

A(:,:)

is the equivalent two-dimensional array. For matrices


this is the same as A.

A(j:k)

is A(j), A(j+1),...,A(k).

A(:,j:k)

is A(:,j), A(:,j+1),...,A(:,k).

A(:,:,k)

is the kth page of three-dimensional array A.

A(i,j,k,:
)

is a vector in four-dimensional array A. The vector


includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.

A(:)

is all the elements of A, regarded as a single column. On


the left side of an assignment statement, A(:) fills A,
preserving its shape from before. In this case, the right

side must contain the same number of elements as A.

Everyforcommandmusthaveamatchingendstatementtoindicatewhichcommandsshouldbeexecutedseveraltimes.
Youcanhavenestedforloops.Forexample:
form=1:3
forn=1:3
x(m,n)=m+n*i;
end
end
definesxtobethematrix:
x=
1.0000+1.0000i1.0000+2.0000i1.0000+3.0000i
2.0000+1.0000i2.0000+2.0000i2.0000+3.0000i
3.0000+1.0000i3.0000+2.0000i3.0000+3.0000i

You might also like