Function with two inputs and arguments

4 次查看(过去 30 天)
Hello I am stuck on a problem. I am supposed to make a function that solves quadratic equations. If however the function gives complex numbers, it is supposed to give me the anwsers x1=888, x2=888. How do I do this?
ax2bxc0.png
x1ochx2.png
if b24ac0.png then xpxm.png
I tried;
function [xp,xm] = find_roots(a,b,c)
if b^2-4*a*c<0
xp=888 & xm=888
else
xp=(-b/(2*a))+sqrt(b^2-4*a*c)/(2*a)
xm=(-b/(2*a))-sqrt(b^2-4*a*c)/(2*a)
end % end function
However it if far from the right anwser. I basically don't know how to do arguments in a function. I even tried the "==" in the if/else statement in my function and it still did not work.
Can anyone help me?
Thanks in advance!

采纳的回答

Fabio Freschi
Fabio Freschi 2019-10-8
编辑:Fabio Freschi 2019-10-8
The & is misused. Try:
function [xp,xm] = find_roots(a,b,c)
if b^2-4*a*c<0
xp=888;
xm=888;
else
xp=(-b/(2*a))+sqrt(b^2-4*a*c)/(2*a);
xm=(-b/(2*a))-sqrt(b^2-4*a*c)/(2*a);
end
Also have a look at the built-in roots
doc roots

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by