Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
solve command give z1

Subject: solve command give z1

From: Autar

Date: 15 Jul, 2011 00:24:12

Message: 1 of 8

I have given a project to my students where they need to use the solve command to solve a nonlinear equation. Depending on how one writes the equation, we get a solution. In other forms, it gives answer as z1 + 1.2566370614359172953850573533118*k*i
This does not make sense when it gives a solution in one form and not the other; it has a real solution that can be found by Newton Raphson method. Please help as it discourages my students.

clc
clear all
a=20
c=3.2
theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
t=[0 5 10 15 20 25 45 60];
n=length(t);
syms b;
sr=0;
for i=1:1:n
    thetapred=a*exp(b*t(i))+c;
    sr=sr+(theta(i)-thetapred)^2;
end
dbsr=diff(sr,b);
dbsr=vpa(dbsr,16);
solve(dbsr,b)

Subject: solve command give z1

From: Roger Stafford

Date: 15 Jul, 2011 03:46:11

Message: 2 of 8

"Autar " <kaw@eng.usf.edu> wrote in message <ivo1bc$bkd$1@newscl01ah.mathworks.com>...
> I have given a project to my students where they need to use the solve command to solve a nonlinear equation. Depending on how one writes the equation, we get a solution. In other forms, it gives answer as z1 + 1.2566370614359172953850573533118*k*i
> This does not make sense when it gives a solution in one form and not the other; it has a real solution that can be found by Newton Raphson method. Please help as it discourages my students.
>
> clc
> clear all
> a=20
> c=3.2
> theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
> t=[0 5 10 15 20 25 45 60];
> n=length(t);
> syms b;
> sr=0;
> for i=1:1:n
> thetapred=a*exp(b*t(i))+c;
> sr=sr+(theta(i)-thetapred)^2;
> end
> dbsr=diff(sr,b);
> dbsr=vpa(dbsr,16);
> solve(dbsr,b)
- - - - - - - - - - -
  It is one thing to solve a problem like this one using some numerical iteration technique such as the Newton Raphson method, but it is quite another to request a precise analytical solution to it. You are lucky you received an answer at all. I wouldn't know how to even start on a problem like that with pen and paper in a search for an exact mathematical expression as solution.

  Perhaps you thought that by doing 'vpa' on 'dbsr' in the next to last step you would be telling 'solve' to use some sort of numerical approximation, but my understanding is that this is not what happens. It still attempts to find the precise mathematical solution to whatever it is given and it is quite remarkable in my opinion that it was able to do so even part of the time. (In those cases that ended in a complex answer the poor thing was probably too exhausted to look any further for real roots.)

  In any case this is not the kind of problem you should be giving your students as examples of use with a symbolic function such as 'solve'. It is inappropriate for this kind of problem. For these you should be teaching them about the various iterative numerical methods to use, such as matlab's 'fzero'.

Roger Stafford

Subject: solve command give z1

From: Roger Stafford

Date: 15 Jul, 2011 04:32:09

Message: 3 of 8

"Roger Stafford" wrote in message <ivod63$aek$1@newscl01ah.mathworks.com>...
> "Autar " <kaw@eng.usf.edu> wrote in message <ivo1bc$bkd$1@newscl01ah.mathworks.com>...
> > t=[0 5 10 15 20 25 45 60];
> > n=length(t);
> > syms b;
> > sr=0;
> > for i=1:1:n
> > thetapred=a*exp(b*t(i))+c;
> > sr=sr+(theta(i)-thetapred)^2;
> > end
> > dbsr=diff(sr,b);
> > dbsr=vpa(dbsr,16);
> > solve(dbsr,b)
> - - - - - - - - - - -
> ...... I wouldn't know how to even start on a problem like that with pen and paper in a search for an exact mathematical expression as solution. .......
- - - - - - - - -
  I have to modify my statement a bit. I do know how to reduce that problem to that of solving for the roots of a polynomial equation, in this case one of a one hundred and twentieth degree, by considering an unknown x as x = exp(b), or if solve were very clever, only a twenty-fourth degree polynomial with x = exp(5*b). In any case there would be a great many roots to deal with, the overwhelming majority of them being complex-valued.

  I still regard it remarkable that 'solve' was able to give you any solutions at all and I still consider it an inappropriate problem for symbolic solution.

Roger Stafford

Subject: solve command give z1

From: Roger Stafford

Date: 15 Jul, 2011 04:42:09

Message: 4 of 8

"Roger Stafford" wrote in message <ivofs9$gm5$1@newscl01ah.mathworks.com>...
> I have to modify my statement a bit. I do know how to reduce that problem to that of solving for the roots of a polynomial equation, in this case one of a one hundred and twentieth degree, by considering an unknown x as x = exp(b), or if solve were very clever, only a twenty-fourth degree polynomial with x = exp(5*b). In any case there would be a great many roots to deal with, the overwhelming majority of them being complex-valued.
>
> I still regard it remarkable that 'solve' was able to give you any solutions at all and I still consider it an inappropriate problem for symbolic solution.
>
> Roger Stafford
- - - - - - - - -
  One more modification. There will actually be infinitely many roots since the complex logarithm function has infinitely many branches. It is no wonder that 'solve' became confused and gave you only one.

Roger Stafford

Subject: solve command give z1

From: Autar

Date: 15 Jul, 2011 23:52:28

Message: 5 of 8

Thanks for the input. I found a solution to the problem. I was not looking for an exact solution but the unique (from the physics of the problem) real solution this problem has. I wanted to avoid having to use a separate function file to use fzero or fsolve etc. But by using vectorizing, inline and char, we can circumvent the problem.

clc
clear all
a=20
c=3.2
theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
t=[0 5. 10. 15. 20. 25. 45. 60.];
n=length(t);
syms b;
sr=0;
for i=1:1:n
    thetapred=a*exp(b*t(i))+c;
    sr=sr+(theta(i)-thetapred)^2;
end
dbsr=diff(sr,b);
F = vectorize(inline(char(dbsr)))
fsolve(F, -2.0)

Subject: solve command give z1

From: Roger Stafford

Date: 16 Jul, 2011 00:55:29

Message: 6 of 8

"Autar " <kaw@eng.usf.edu> wrote in message <ivqjrs$ncd$1@newscl01ah.mathworks.com>...
> Thanks for the input. I found a solution to the problem. I was not looking for an exact solution but the unique (from the physics of the problem) real solution this problem has. I wanted to avoid having to use a separate function file to use fzero or fsolve etc. But by using vectorizing, inline and char, we can circumvent the problem.
>
> clc
> clear all
> a=20
> c=3.2
> theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
> t=[0 5. 10. 15. 20. 25. 45. 60.];
> n=length(t);
> syms b;
> sr=0;
> for i=1:1:n
> thetapred=a*exp(b*t(i))+c;
> sr=sr+(theta(i)-thetapred)^2;
> end
> dbsr=diff(sr,b);
> F = vectorize(inline(char(dbsr)))
> fsolve(F, -2.0)
- - - - - - - - - - - -
  You do have another method available to you, Autar. You can rewrite your code to be a twenty-fourth degree polynomial in terms of x = exp(5*b) equivalent to the current derivative, 'dbsr', much as you have done in forming 'dbsr'. You can use 'collect' to obtain expressions for each of its coefficients. That will then allow you to apply the 'roots' function, from which you can easily eliminate all but its real positive roots. By taking the logarithm and dividing by 5 of the remaining root(s), you can then arrive at your desired solution for b. No need for 'vectorize'. No need for iteration with 'fsolve'.

  An alternative to the above is to form the polynomial version of 'sr' again with x = exp(5*b), and use its derivative taken with respect to 'x' rather than with respect to 'b'. The coefficients should be somewhat simpler. Then proceed to find the roots of this somewhat different derivative as was done above.

  The form of your problem suggests that you have been trying to find extrema, presumably minima, for the original sum of squares in 'sr' by varying b. If you are willing to use iteration, I would think that 'lsqnonlin' of the optimization toolbox would be ideal for the purpose.

Roger Stafford

Subject: solve command give z1

From: Christopher Creutzig

Date: 20 Jul, 2011 11:06:09

Message: 7 of 8

"Autar " <kaw@eng.usf.edu> wrote in message <ivqjrs$ncd$1@newscl01ah.mathworks.com>...
> Thanks for the input. I found a solution to the problem. I was not looking for an exact solution but the unique (from the physics of the problem) real solution this problem has. I wanted to avoid having to use a separate function file to use fzero or fsolve etc. But by using vectorizing, inline and char, we can circumvent the problem.

Try telling the system you want a real solution, e.g., by assuming that b is real. The following works just fine in 11a:

clc
clear all
a=20
c=3.2
theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
t=[0 5. 10. 15. 20. 25. 45. 60.];
n=length(t);
syms b real;
sr=0;
for i=1:1:n
    thetapred=a*exp(b*t(i))+c;
    sr=sr+(theta(i)-thetapred)^2;
end
dbsr=diff(sr,b);
solve(dbsr,b)

Subject: solve command give z1

From: Autar

Date: 21 Jul, 2011 19:19:09

Message: 8 of 8

This is perfect. It worked. Straightforward and simple solution.
Autar

"Christopher Creutzig" <christopher.creutzig@mathworks.de> wrote in message <j06cr1$nd0$1@newscl01ah.mathworks.com>...
> "Autar " <kaw@eng.usf.edu> wrote in message <ivqjrs$ncd$1@newscl01ah.mathworks.com>...
> > Thanks for the input. I found a solution to the problem. I was not looking for an exact solution but the unique (from the physics of the problem) real solution this problem has. I wanted to avoid having to use a separate function file to use fzero or fsolve etc. But by using vectorizing, inline and char, we can circumvent the problem.
>
> Try telling the system you want a real solution, e.g., by assuming that b is real. The following works just fine in 11a:
>
> clc
> clear all
> a=20
> c=3.2
> theta=[22.8 17.7 15.5 12.9 11.9 10.3 6.6 6.2];
> t=[0 5. 10. 15. 20. 25. 45. 60.];
> n=length(t);
> syms b real;
> sr=0;
> for i=1:1:n
> thetapred=a*exp(b*t(i))+c;
> sr=sr+(theta(i)-thetapred)^2;
> end
> dbsr=diff(sr,b);
> solve(dbsr,b)

Tags for 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.

Contact us