USING MATLAB Problem 2. Create a USER DEFINED FUNCTION that finds the roots of a
ID: 2248067 • Letter: U
Question
USING MATLAB
Problem 2. Create a USER DEFINED FUNCTION that finds the roots of a quadratic equation. Required Conditions 1) 2) 3) We need to consider three cases (Repeating Real Roots, Non repeating real roots, and comples conjugate roots) The output of disp functions should include text strings and numerical results together (use num2str Add Comment that can be displayed for the user when the user type help name_of_user function Use the following screenshot as a guide line. >> help quadratic adv This function, will find the rots f a quadratic function This function will have different message displayed based on the nature of the roots Example x1,x2-quadratic adv (a, b,c) > [xi,x2]-quadratic adv (a,b,c) This quadratic equation wil1 have repeating real roots The value of the repeating root is -2 -2 -2 > 3 >[x1,x2)-quadratic adv (a,b, c) This quadratie equation will have non repeating real roots The value of the first root is -1 and the value of the second root is -3 X1 . >> [X1,X2-quadratic-advia,b,c) This quadratic equation will have Complex Conjugate root The value of the first root is -2+1i and the value of the second root is-2-1i x1 = 2.0000 +1.0000 2.0000-1.00005Explanation / Answer
Matlab Script:
function [X1 X2] = quadratic_adv(a,b,c)
%This function will find the roots of a quadratic function.
%This function will have different message displayed based on
%the nature of roots.
%Example
%a=1;b=4;c=5;
%[X1 X2]=quadratic_adv(a,b,c)
X = roots([a b c]);
X1 = X(1);
X2 = X(2);
X1_str = num2str(X1);
X2_str = num2str(X2);
if X1==X2
disp('This quadratic equation will have repeating real roots.');
disp(['The value of repeating root is ' X1_str]);
elseif X1~=X2 && imag(X1)==0 && imag(X2)==0
disp('This quadratic equation will have non repeating real roots.');
disp(['The value of first root is ' X1_str ' and the value of second root is ' X2_str]);
else
disp('This quadratic equation will have complex conjugate roots.');
disp(['The value of first root is ' X1_str ' and the value of second root is ' X2_str]);
end
end
output plot:
>> help quadratic_adv
This function will find the roots of a quadratic function.
This function will have different message displayed based on
the nature of roots.
Example
a=1;b=4;c=5;
[X1 X2]=quadratic_adv(a,b,c)
>> a = 1;b=4;c=4;
>> [X1 X2] = quadratic_adv(a,b,c)
This quadratic equation will have repeating real roots.
The value of repeating root is -2
X1 =
-2
X2 =
-2
>> c=3;
>> [X1 X2] = quadratic_adv(a,b,c)
This quadratic equation will have non repeating real roots.
The value of first root is -3 and the value of second root is -1
X1 =
-3
X2 =
-1
>> c=5;
>> [X1 X2] = quadratic_adv(a,b,c)
This quadratic equation will have complex conjugate roots.
The value of first root is -2+1i and the value of second root is -2-1i
X1 =
-2.0000 + 1.0000i
X2 =
-2.0000 - 1.0000i
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.