Question

What's wrong with my MatLab script: function [x1, x2]=quadratic_adv(a,b,c) s= (b^2)-(4*a*c); x1= (-b + (sqrt((b^2) -...

What's wrong with my MatLab script:

function [x1, x2]=quadratic_adv(a,b,c)
s= (b^2)-(4*a*c);
x1= (-b + (sqrt((b^2) - (4*a*c))))/(2*a);
x2= (-b - (sqrt((b^2) - (4*a*c))))/(2*a);

if x1==x2
disp('The quadratic equation will have repeating real roots. The value of the roots are ',num2str(x1),'and',num2str(x2))
elseif x1~=x2 && s>0
disp('The quadratic equation will have nonrepating real roots. The value of the first root is ',num2str(x1),' The value of the second root is',num2str(x2))
elseif s<0
disp('The quadratic equation will have complex conjugate roots. The value of the first root is ',num2str(x1),'The value of the second root is',num2str(x2))
end

Practice Problem:

Problem 2. Create a USER DEFINED FUNCTION that finds the roots of a quadratic equation.

Required Conditions

1)

We need to consider three cases ( Repeating Real Roots, Non repeating real roots,

and comples conjugate roots)

2)

The output of disp functions should include text strings and numerical results

together (use num2str)

3)

Add Comment that can be displayed for the user when the user type

help name_of_user_function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Please be informed that, only one variable can be displayed in a "disp" command. If elements of two variables need to be displayed together, a new variable (that contains the elements to be displayed) has to be defined separately.

Please use the code given below (comment section for "help" as per 3rd section of the question is also included):

function quadratic_adv(a,b,c)
%This function is to calculate the roots of a quadratic equation
%Input arguments:
%Coefficients of the quadratic equation: a,b,c (for the eqaution in the form ax^2+bx+c)
%Output arguments:
%Roots of the quadratic equation (x1, x2).
s= (b^2)-(4*a*c);
x1= (-b + (sqrt((b^2) - (4*a*c))))/(2*a);
x2= (-b - (sqrt((b^2) - (4*a*c))))/(2*a);

if (x1==x2)
disp('The quadratic equation will have repeating real roots. Value of the roots are: ')
disp ('x1:')
disp(num2str(x1))
disp ('x2:')
disp(num2str(x2))
elseif ((x1~=x2) && (s>0))
disp('The quadratic equation will have nonrepeating real roots.')
disp('Value of the first root, x1 is: ')
disp(num2str(x1))
disp(' Value of the second root x2 is:'),
disp(num2str(x2))
elseif (s<0)
disp('The quadratic equation will have complex conjugate roots.')
disp('Value of the first root x1 is: ')
disp(num2str(x1))
disp('Value of the second root x2 is:')
disp(num2str(x2))
end

Note:

[x1,x2] from the function header is removed to suppress the "ans" line appearing in the Matlab output.

You can put it back, if this is not a problem.

Please let me know, in case of any clarification.

Add a comment
Know the answer?
Add Answer to:
What's wrong with my MatLab script: function [x1, x2]=quadratic_adv(a,b,c) s= (b^2)-(4*a*c); x1= (-b + (sqrt((b^2) -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT