1- Wirite a user-deined REATLAD function which takes the coefficents of the quad
ID: 1867894 • Letter: 1
Question
1- Wirite a user-deined REATLAD function which takes the coefficents of the quadraic equation az"brtem0 as imputs and refurns its roots and 22 as outputs. For function name and arguments, use x1, x21-myquad1(a, b, c) 2- Repeat the procedure, but thes time write an anonymous MATLAB function called myquad2 which takes a. b and c as inputs and returms the roots ina2 x 1 column vector as he output 3 Test your functions to find the roots of 3z see if they retum the correct answer +5z-1-0 and 0.1805 -1.8471 Note: Pertorm sanity check in your user-defined function to make sure that real roots exist Otherwise, return a warning message and assign NaN to z1 and z 82Explanation / Answer
1)
%MAIN PROGRAM
clc
clear all
[x1,x2]=myquad1(1,4,4);
display('Roots are:');
display([x1,x2]);
%MYQUAD1 Function
function [x1,x2]=myquad1(a,b,c)
d=b^2-4*a*c;
f=sqrt(b^2-4*a*c);
if d>=0
x1=(-b+f)/(2*a);
x2=(-b-f)/(2*a);
else
display('give correct coefficients');
x1=NaN;
x2=NaN;
end
output
Roots are:
ans =
-2 -2
2)
%MAIN PROGRAM
clc
clear all
[x1,x2]=myquad2(1,4,4);
display('Roots are:');
display([x1,x2]');
%MYQUAD2 Function
function [x1,x2]=myquad2(a,b,c)
d=b^2-4*a*c;
f=sqrt(b^2-4*a*c);
if d>=0
x1=(-b+f)/(2*a);
x2=(-b-f)/(2*a);
else
display('give correct coefficients');
x1=NaN;
x2=NaN;
end
output
Roots are:
ans =
-2
-2
3)
clc
clear all
[x1,x2]=myquad1(3,5,-1);
display('Roots are:');
display([x1,x2]);
function [x1,x2]=myquad1(a,b,c)
d=b^2-4*a*c;
f=sqrt(b^2-4*a*c);
if d>=0
x1=(-b+f)/(2*a);
x2=(-b-f)/(2*a);
else
display('give correct coefficients');
x1=NaN;
x2=NaN;
end
Roots are:
ans =
0.1805 -1.8471
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.