MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Thread Subject: fsolve

 Subject: fsolve From: Pratheeba Date: 2 Jul, 2012 11:58:06 Message: 1 of 11 I am working on a set of equations which are DAE. I use fsolve for the algebraic equations and ode15s for solving the differential equations. But i get complex numbers as output from fval. I tried to change the initial value, slack tolerances, increase number of iterations. But none of them work. The physics of the problem demands a real solution and not complex. How do i avoid getting complex values in fsolve?
 Subject: fsolve From: Nasser M. Abbasi Date: 2 Jul, 2012 12:25:30 Message: 2 of 11 On 7/2/2012 6:58 AM, Pratheeba wrote: > I am working on a set of equations which are DAE. I use fsolve for the algebraic >equations and ode15s for solving the differential equations. But i >get complex numbers as output from fval. I tried to change the >initial value, slack tolerances, increase number of iterations. But none >of them work. The physics of the problem demands a real solution and >not complex. How do i avoid getting complex values in fsolve? > hi; If you get roots that are complex and you do NOT want them to be complex, then just take the real part. help real() If the equation insist in being complex, and you do not want it to give you complex solution in the first place, then you need to change the equations. Sometimes in numerical computations, perturbation occur due to floating point and you can end up with a complex solution. But if it due to this, those solutions would have a very tiny complex part, that you can chop away or just do real() on the solution to clean it up. In analytical solutions, since those are exact, those will not show up. --Nasser
 Subject: fsolve From: Torsten Date: 2 Jul, 2012 12:15:39 Message: 3 of 11 Usually complex values occur if the algebraic equations contain functions which can produce complex results, e.g. log(x) or sqrt(x) for an input argument x<0. Is this true in your case ? Best wishes Torsten.
 Subject: fsolve From: Pratheeba Date: 3 Jul, 2012 09:28:07 Message: 4 of 11 Torsten wrote in message <1d80e824-00c9-4ff7-91e7-6949372b1eed@w24g2000vby.googlegroups.com>... > Usually complex values occur if the algebraic equations contain > functions > which can produce complex results, e.g. log(x) or sqrt(x) for an input > argument x<0. > Is this true in your case ? > > Best wishes > Torsten. Yes It is true that i have sqrt(x) in my function. But physics of the problem insists that the value of x is always real and positive, and at the least it can be zero. Any idea to have a better solver or better initial guess with fsolve?
 Subject: fsolve From: Torsten Date: 3 Jul, 2012 10:26:09 Message: 5 of 11 > > Yes It is true that i have sqrt(x) in my function. But physics of the problem insists that the value of x is always real and positive, and at the least it can be zero. > > Any idea to have a better solver or better initial guess with fsolve? If you know a variable x must be real and positive, you could replace it by x^2 in the algebraic equations to avoid such problems as you describe. Best wishes Torsten.
 Subject: fsolve From: Pratheeba Date: 3 Jul, 2012 11:15:14 Message: 6 of 11 Thanks for your reply. I can't make x^2 as I want. So i tried doing this: yk = phi(1:nspg); yk_guess = yk/4; opts = optimset('TolFun', 1e-6); fval = 0; yks=0; if (isreal(fval)<0)     yk_guess= yk_guess+1e-5; [yks,fval] = fsolve(@algebraic,yk_guess,opts,yk) yks = yks1; end yks1=yks; My intention was to make initial choice better for fsolve. But when i ran the code, i got error message that yks returned by fsolve was a single value and not an array. Does anyone have clue to run fsolve with an array and within while loop? Thanks Torsten wrote in message <136073f7-c3a8-4982-a922-94c9539bceaa@6g2000vbv.googlegroups.com>... > > > > Yes It is true that i have sqrt(x) in my function. But physics of the problem insists that the value of x is always real and positive, and at the least it can be zero. > > > > Any idea to have a better solver or better initial guess with fsolve? > > If you know a variable x must be real and positive, you could replace > it by x^2 in the algebraic equations > to avoid such problems as you describe. > > Best wishes > Torsten. >
 Subject: fsolve From: Pratheeba Date: 3 Jul, 2012 11:16:09 Message: 7 of 11 Thanks for your reply. I can't make x^2 as I want. So i tried doing this: yk = phi(1:nspg); yk_guess = yk/4; opts = optimset('TolFun', 1e-6); fval = 0; yks=0; if (isreal(fval)<0)     yk_guess= yk_guess+1e-5; [yks,fval] = fsolve(@algebraic,yk_guess,opts,yk) yks = yks1; end yks1=yks; My intention was to make initial choice better for fsolve. But when i ran the code, i got error message that yks returned by fsolve was a single value and not an array. Does anyone have clue to run fsolve with an array and within while loop? Thanks Torsten wrote in message <136073f7-c3a8-4982-a922-94c9539bceaa@6g2000vbv.googlegroups.com>... > > > > Yes It is true that i have sqrt(x) in my function. But physics of the problem insists that the value of x is always real and positive, and at the least it can be zero. > > > > Any idea to have a better solver or better initial guess with fsolve? > > If you know a variable x must be real and positive, you could replace > it by x^2 in the algebraic equations > to avoid such problems as you describe. > > Best wishes > Torsten. >
 Subject: fsolve From: Torsten Date: 3 Jul, 2012 12:18:23 Message: 8 of 11 I don't understand what you are trying to do. 1. isreal(...) can give 0 or 1 - why do you check whether it's <0 ? 2. You set fval=0 - then the following if-statement if(isreal(fval) ... is always not satisfied. 3. Why do you improve the initial guess for yk_guess by adding 1e-5 ? 4. Your call to fsolve should be [yks,fval] = fsolve(@(y)algebraic(y,yk),yk_guess,opts). ... Best wishes Torsten.
 Subject: fsolve From: Pratheeba Date: 3 Jul, 2012 13:02:11 Message: 9 of 11 As you told, isreal(fval) will return 1 or 0 if real or complex respectively. I wrote wrongly as if(isreal(fval)<0), it should be if(isreal(fval)<1).  if this condition is true then, my initial guess should change by 1e-5. 1e-5 is the increment because the starting values or guesses are in that order. the last statement of yours is not clear to me. can you elaborate on that? Thanks Torsten wrote in message <2c8711de-0408-4994-9f04-604abd7c8433@n33g2000vbi.googlegroups.com>... > I don't understand what you are trying to do. > > 1. isreal(...) can give 0 or 1 - why do you check whether it's <0 ? > 2. You set fval=0 - then the following if-statement > if(isreal(fval) ... is always not satisfied. > 3. Why do you improve the initial guess for yk_guess by adding 1e-5 ? > 4. Your call to fsolve should be [yks,fval] = > fsolve(@(y)algebraic(y,yk),yk_guess,opts). > ... > > Best wishes > Torsten. > >
 Subject: fsolve From: Nasser M. Abbasi Date: 3 Jul, 2012 13:45:15 Message: 10 of 11 On 7/3/2012 8:02 AM, Pratheeba wrote: > As you told, isreal(fval) will return 1 or 0 if real or complex respectively. >I wrote wrongly as if(isreal(fval)<0), it should be if(isreal(fval)<1). I think you are really confused. It should be just   if isreal(number)      do something when number is real   else     do something when number is not real   end not   if isreal(number<1) isreal() already returns a BOOLEAN. i.e. a LOGICAL. BOOLEAN is either TRUE or FALSE nothing else. BOOLEAN is not scalar. But it looks like a number because Matlab makes everything a number. ------------------------------ EDU>> x=isreal(1) x =       1 EDU>> class(x) ans =     logical x =1 EDU>> class(x) ans = double --------------------- The problem is that languages like Matlab and other weakly typed quick protyping languages is that they allow people to mushy things and write nilly willy code that 'works' even if it makes no sense at all. This is because there are no types associated with values that are enforced, and the languages are so forgiving. So one can add a string to a floating point or a add a BOOLEAN to a number and it 'works' even it makes no sense. EDU>> 'micky mouse' + 4 ans =     113 109 103 111 125 36 113 115 121 119 105 --Nasser
 Subject: fsolve From: Marcelo Marazzi Date: 5 Jul, 2012 18:24:18 Message: 11 of 11 Hi Pratheeba: On way to enforce that x be non-negative is to use the lsqnonlin solver. This solver--unlike fsolve--takes bound on the variables; through these bounds you can specify that x>=0. -Marcelo On 7/3/2012 5:28 AM, Pratheeba wrote: > Torsten wrote in message > <1d80e824-00c9-4ff7-91e7-6949372b1eed@w24g2000vby.googlegroups.com>... >> Usually complex values occur if the algebraic equations contain >> functions >> which can produce complex results, e.g. log(x) or sqrt(x) for an input >> argument x<0. >> Is this true in your case ? >> >> Best wishes >> Torsten. > > Yes It is true that i have sqrt(x) in my function. But physics of the > problem insists that the value of x is always real and positive, and at > the least it can be zero. > > Any idea to have a better solver or better initial guess with fsolve?

## Tags for this Thread

No tags are associated with this thread.

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.