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 comentarios

Dyuman Joshi
Dyuman Joshi el 25 de Sept. de 2022
Why have you written the code for condition a==0 twice?
You have to include the code for condition d<0 as well.
jun
jun el 25 de Sept. de 2022
where include and how?,
i include this statements
"else if (d<0)&&(a=0)
[x1 x2] = []
else
[x1 x2] = []" which are next of condition 'a==0',
but it works f(1,0,-4)=-inf
Dyuman Joshi
Dyuman Joshi el 25 de Sept. de 2022
Do you want to return empty values for if any/both of the two conditions - a==0 , d<0?
jun
jun el 25 de Sept. de 2022
i want d<0 => empty value return
but a=0&d<0 => x1=x2=-c/b return that i want

Iniciar sesión para comentar.

 Respuesta aceptada

VBBV
VBBV el 25 de Sept. de 2022
if (d>=0) & (~(a==0))
use binary operator &

11 comentarios

[x1 x2] = f(4.1,10,4)
d = 34.4000
x1 = -1.9348
x2 = -0.5042
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
end
Missing end keyword in the function
jun
jun el 25 de Sept. de 2022
Maybe in your matlab, besides plotting, does the output of exactly what the values ​​of x1 and x2 are?
I'm using octave now, and your code only displays the result of x1. but ploting is well
it's octave problem..?
VBBV
VBBV el 25 de Sept. de 2022
It displays both x1 and X2 values with red markers on plot.
VBBV
VBBV el 25 de Sept. de 2022
Editada: VBBV el 25 de Sept. de 2022
Since a is not zero in demonstrated case function does not execute first if end condition
jun
jun el 25 de Sept. de 2022
I'm not good at English, so I'll ask again, In my matlab in octave ver, when f(1,0,-4), it doesn't come out as x1=-2 and x2=2, but derived as ans=-2. However, in plotting, both solutions work well. Does your matlab yield x1=-2,x2=2?
[x1 x2] = f(1,0,-4)
d = 16
x1 = -2
x2 = 2
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
end
VBBV
VBBV el 25 de Sept. de 2022
x1 and x2 values are -2 and 2 from above result. What do you get as result ?
jun
jun el 25 de Sept. de 2022
result appers x1, but figure graph is well,
Dyuman Joshi
Dyuman Joshi el 25 de Sept. de 2022
Why are you repeating a piece of code? It's redundant.
jun
jun el 26 de Sept. de 2022
Are you right about 'a==0'? If 'a=0', an error like this is displayed
Let me highlight it. See the green parts, both are same.
I would suggest you to remove the top one and edit last else as
elseif d<0
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
end

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

jun
el 25 de Sept. de 2022

Comentada:

el 26 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by