solve command give z1

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.

"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" 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" 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.

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.

"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.

"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:

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)

What is a watch list?

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

How do I add an item to my watch list?

Search

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

Author

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

Thread

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

About Newsgroups, Newsreaders, and MATLAB Central

What are newsgroups?

The newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

How do I read or post to the newsgroups?

MATLAB Central

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

Use the Email Address of Your Choice
The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

Spam Control
Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

Tagging
Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

Watch lists
Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

Other ways to access the newsgroups

Use a newsreader through your school, employer, or internet service provider

Pay for newsgroup access from a commercial provider

Use Google Groups

Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup