Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

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:current/sad-b-sqrt(d))/(2*a);
    x2:current/sad-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  current/sad  along copy paste

Re: I think it's simple

CTG,

  thank you a loooooot it was helpful for me and for who has the same problem

There are 0 guests and 0 other users also viewing this topic