how to write a function for quadratic equation?
古いコメントを表示
I wrote this into matlab but it doesn't work where express x1, x2, it express only one sol in d>=0 but ploting is work in d>=0. and it's error in d<0
why it doen't work and how to fix it?
function [x1,x2] = f(a,b,c)
if a==0
x1=-c/b;
x2=-c/b;
plot(x1,0,'gx',x2,0,'gx')
hold on
fplot(@(x) a*x.^2+b*x+c)
hold off
end
d=b^2-4*a*c;
if (d>=0)&&(~(a==0))
x1=((-b-sqrt(d))/(2*a));
x2=((-b+sqrt(d))/(2*a));
plot(x1,0,'rx',x2,0,'rx')
hold on
fplot(@(x) a*x.^2+b*x+c)
hold off
elseif a==0
x1=-c/b;
x2=-c/b;
plot(x1,0,'gx',x2,0,'gx')
hold on
fplot(@(x) a*x.^2+b*x+c)
hold off
else
[x1 x2]=[]
end
4 件のコメント
Dyuman Joshi
2022 年 9 月 25 日
Why have you written the code for condition a==0 twice?
You have to include the code for condition d<0 as well.
jun
2022 年 9 月 25 日
Dyuman Joshi
2022 年 9 月 25 日
Do you want to return empty values for if any/both of the two conditions - a==0 , d<0?
jun
2022 年 9 月 25 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Transforms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



