need help Writing a function that returns roots of a quadratic

This is my plan on how to write it (don't have software at my disposal currently). I am learning matlab so understand there may be quite a few problems.
Function [x]=myquad(a,b,c)
x=[r1 r2];
d=sqrt((b.^2)-4.*a.*c)
if a==0 & b>0
r1=-c/b;
elseif a==0 & b==0
r1=0;
elseif a~=0 & d==0
r1=-b./(2.*a);r2=r1;
elseif a~=0 & d>0
r1=(-b+d)./2a; r2=(-b+d)./2.*a;
elseif a~=0 & d<0
r1=(-b+d.*i)/2a; r2=(-b-d.*i)./2.*a;
end
end
I need to follow this algorithm for an assignment.
THANKS!

 Accepted Answer

Your first two cases do not appear to set r2 to anything.
your a=0 b=0 case is wrong
Your "di" should probably be "d*i"

6 Comments

There is no r2 in that case because the function would not be a true quadratic. Do I just assume the user knows that so they only use one variable to store the output when calling the function? or is there a way for matlab to tell the user this?
If you define an output and do not assign any value to it, then MATLAB will give you an error message. You have to assign something, even if it is the empty array.
The usual way of handling this is to define the function to return only one variable, and set that one variable to be a vector containing all of the roots -- so only one element if there is only one root.
I just edited it to that way. Thanks for the help. do you see anything else wrong with the code?
also what is the difference between a==0 and b==0 having 2 equal signs where as it is ok if d=0 has just one. Does it have to do with a and b being user inputs?
a==0 is a logical expression, it can be true or false while d=0 is an affectation.
Try this in windows command
a=10 % it's an affectation the result is 10
a==20 % it's a logical expression, the result is 0 (false)
a==10 % it's a logical expression, the result is 1 (true)
and
d=10 % it's an affectation the result will be 10
then that means my "d=0" should also be "d==0" in the code as well, right?

Sign in to comment.

More Answers (2)

There are some mistakes
if a==0 %instead of if a=0
elseif % instead of ifelse

1 Comment

Multiple assignment is not permitted. r1=r2=VALUE is not allowed. Use r1=VALUE; r2=r1

Sign in to comment.

Why do you need all those "if"s at all? Why not just do
function [r1 r2]=myquad(a,b,c)
d = sqrt((b^2)-4*a*c); % Can be complex with no problem.
r1=(-b-d)/(2*a);
r2=(-b+d)/(2*a);
MATLAB handles complex numbers and infinite numbers with no problem, so you don't need special if statements to do it manually.

2 Comments

Probably would make more sense to do that but I have to address all the different cases for an assignment. For instance if a user entered myquad(0,1,2) then I need it to say. "Not a quadratic, r1=-1/2".
do you think I could just write fprintf statements underneath the logical expressions for the different cases, in place of the formulas, and use what you suggested?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!