Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum > General > > "Plotting non-linear values and solving"

Post Reply:
Name:
Email Address:

Please send email when my message is replied to.

Url (optional):
Subject:
Message: view original message?
Attachment (optional):
Please answer this:1+4 =



Original Message (ID '68849') By Bill Simpson:
First we combine your y and b equations into a single equation. Unfortunately if you look very carefully at your post you can see there are some mismatched ( and } and that is often a bad sign, but I guess how to patch that and see what happens. In[1]:= y=(b-(b^2(2K^2(0.000011501(x)))/s))^.5/(2K(0.000011501))/.b->1+(K(0.000011501))+(K(x)/(2s)) Out[1]= (43474.48047995826*(1 + 0.000011501*K + (K*x)/(2*s) + (0.000023002*K^2*x)/s - (1 + 0.000011501*K + (K*x)/(2*s))^2)^0.5)/K That expression/.variable->anotherexpression is Mathematica notation for substituting a new expression for a variable in an old expression. Now for a first attempt, I'll assume all the error is in y and find the sum of squared error. In[2]:= data={ {0.0000079,0.244},{0.0000119,0.295},{0.0000158,0.428},{0.0000198,0.488},{0.0000237,0.703},{0.0000277,0.711},{0.0000316,0.859},{0.0000356,0.878},{0.0000395,0.926},{0.0000435,0.915},{0.0000474,0.991},{0.0000593,1.04},{0.0000632,1.02}}; Total[Map[(#[[2]]-y/.x->#[[1]])^2&,data]] Out[3]= (0.244 - (43474.5*(1 + 0.000011501*K - (1 + 0.000011501*K + (3.95*^-6 *K)/s)^2 + (3.95*^-6*K)/s + (1.81716*^-10*K^2)/s)^0.5)/K)^2 + (0.295 - (43474.5*(1 + 0.000011501*K - (1 + 0.000011501*K + (5.95*^-6 *K)/s)^2 + (5.95*^-6*K)/s + (2.73724*^-10*K^2)/s)^0.5)/K)^2 + (0.428 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (7.9*^-6 *K)/s)^2 + (7.9*^-6*K)/s + (3.63432*^-10*K^2)/s)^0.5)/K)^2 + (0.488 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (9.9*^-6 *K)/s)^2 + (9.9*^-6*K)/s + (4.5544*^-10*K^2)/s)^0.5)/K)^2 + (0.703 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.00001185*K)/s)^2 + (0.00001185*K)/s + (5.45147*^-10*K^2)/s)^0.5)/K)^2 + (0.711 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.00001385*K)/s)^2 + (0.00001385*K)/s + (6.37155*^-10*K^2)/s)^0.5)/K)^2 + (0.859 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.0000158*K)/s)^2 + (0.0000158*K)/s + (7.26863*^-10*K^2)/s)^0.5)/K)^2 + (0.878 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.0000178*K)/s)^2 + (0.0000178*K)/s + (8.18871*^-10*K^2)/s)^0.5)/K)^2 + (0.926 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.00001975*K)/s)^2 + (0.00001975*K)/s + (9.08579*^-10*K^2)/s)^0.5)/K)^2 + (0.915 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.00002175*K)/s)^2 + (0.00002175*K)/s + (1.00059*^-9*K^2)/s)^0.5)/K)^2 + (0.991 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.0000237*K)/s)^2 + (0.0000237*K)/s + (1.09029*^-9*K^2)/s)^0.5)/K)^2 + (1.04 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.00002965*K)/s)^2 + (0.00002965*K)/s + (1.36402*^-9*K^2)/s)^0.5)/K)^2 + (1.02 - (43474.5* (1 + 0.000011501*K - (1 + 0.000011501*K + (0.0000316*K)/s)^2 + (0.0000316*K)/s + (1.45373*^-9*K^2)/ s)^0.5)/K)^2 To explain what just happened, this: Total[Map[(#[[2]]-y/.x->#[[1]])^2&,data]] is going to take all your pairs of numbers, subtract your y equation from the second number of the pair after having substituted in the first number of the pair for x, square the result and then finally total all these squares up. Now we would like to find the K and s that minimizes that total. To do that we just wrap a minimizing function around this, like this: In[4]:= NMinimize[Total[Map[(#[[2]]-y/.x->#[[1]])^2&,data]],{K,s}] From In[4]:= NMinimize::nrnum: "The function value -6.18931*^6-13829.7I is not a real number at {K, s} = {0.448605, 0.161768}. That error message says that trying to find some values for K and s ended up, since you have square roots in your y function, generating a complex number and the minimize function bailed out. At this point I am unsure what to do. It might be one of my guesses about your mismatched ( and }. It might be that K and s must be constrained to lie within certain domains. I don't know. Just to show you how this might finish, I will patch this. I will minimize the Norm of the sum. That will get rid of the complex problem, but will almost certainly give an incorrect fit. In[5]:= NMinimize[Norm[Total[Map[(#[[2]]-y/.x->#[[1]])^2&,data]]],{K,s}] Out[5]= {2.19136,{K->7539.55,s-> -0.546563}} So the total normed error of the fit is 2.19, we want that to be small for a good fit, and the best parameters it could find were K==7539 and s==.546. We don't really know how bad the fit was so lets take a look at the predicted result. In[6]:= fitdata=Map[{#[[1]],Re[y/.x->#[[1]]/.{K->7316.1355923366555`,s-> -0.5571515160707664`}]}&,data] Out[6]= {{7.9`*^-6, 0.`}, {0.0000119`, 0.`}, {0.0000158`, 0.`}, {0.0000198`, 0.`}, {0.0000237`, 0.7026894726009186`}, {0.0000277`, 0.975758865232444`}, {0.0000316`, 1.162790590689777`}, {0.0000356`, 1.3092799163268964`}, {0.0000395`, 1.4214058500321247`}, {0.0000435`, 1.5120600534802078`}, {0.0000474`, 1.5807309679483001`}, {0.0000593`, 1.6913185763914562`}, {0.0000632`, 1.6986322461116352`}} If you compare that with your original data you can see that this is a terrible fit. Now your task is to go back and see if you can identify where things went wrong. Was it my guess what to do with the mismatched ( and }? Was it an error in the equation for y and b? Was it my assumption that all the error lie in y? Was it that minimizing the sum of squared error was the criteria to use? Now go back through this, check each assumption, look for any typos, mine or yours, look up Mathematica keywords in the help system, look at examples of using them there, and see if you can find any mistakes that need to be corrected. You might try repeating all these steps on some much simpler manufactured data with a simpler manufactured y function and verify that the method works for simple cases. Then gradually make things more complicated and see if it continues to work. If you get stuck then make another post with your code and your data and what you need next.