- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » I think it's simple
I think it's simple
I think it's simple
USER wrote:
sjoelund.se
hi
I want to solve this equation ax2+bx+c=0 in OpenModelica and when the discriminant (delta<0) is less then 0 the program does not give me the complex numbers results
Can you help me please?
Code:
function equation_2nd
input Real a;
input Real b;
input Real c;
output Real x1;
output Real x2;
output Real x0;
output Real c1;
output Real c2;
protected
Real d;
constant Complex j;
algorithm
d:=b^2-4*a*c;
if d>0 then
x1:=(-b-sqrt(d))/(2*a);
x2:=(-b+sqrt(d))/(2*a);
elseif d==0 then
x0:=-b/(2*a);
else
c1:=(-b/(2*a))-j*(sqrt(d)/(2*a));
c2:=(-b/(2*a))+j*(sqrt(d)/(2*a));
end if;
end equation_2nd;
Re: I think it's simple
I hope this can help:
function equation_2nd
input Real a;
input Real b;
input Real c;
output Real x1;
output Real x2;
output Real x0;
output Complex c1;
output Complex c2;
protected
Real d;
algorithm
d:=b^2-4*a*c;
if d>0 then
x1:-b-sqrt(d))/(2*a);
x2:-b+sqrt(d))/(2*a);
elseif d==0 then
x0:=-b/(2*a);
else
c1:=Complex((-b/(2*a)),(sqrt(-d)/(2*a)));
c2:=Complex((-b/(2*a)),-(sqrt(-d)/(2*a)));
end if;
end equation_2nd;
Regards
Carlos
Re: I think it's simple
i do not why = was replaced by along copy paste
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » I think it's simple