Message Boards Message Boards

0
|
6206 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Need help to fix the syntax error in plotting

Posted 10 years ago

hi, I am working on a model. I tried to fit the model to the data. After fitting the model, I tried to plot the graph, but it is showing some error. Please help me to fix the problem. Attached the file for reference.

Thanks

Thanks, Jaydeep

Attachments:
POSTED BY: Jaydeep Yadav
8 Replies
Posted 10 years ago

I was unable to find any difference between the two. I also went through your previous comment where you showed the code for both the problems. But I was unable to find the difference. In case if you know the difference,please let me know that might be the reason why one of them is not working.

Thanks

POSTED BY: Jaydeep Yadav
Posted 10 years ago

I am trying to get across to you the simplest methods I know to try to debug programs.

If you claim you have two programs that solve similar sorts of problems and one works and one does not then you try to identify the differences between the two programs. If you think one is correct then perhaps you can see how you are doing something differently in the other program and that might be why it doesn't work.

When I look at those two programs I see that you have done some things in two different ways. I then wonder if that is why one of them is not working.

If you can get both of them working the same way then you can later try to think how to test the output to determine if it is exactly correct.

POSTED BY: Bill Simpson
Posted 10 years ago

Hi, I did not get you? What are you trying to say? Are you trying to say that both the codes are wrong or there is no difference between the code?

Thanks

POSTED BY: Jaydeep Yadav
Posted 10 years ago

Compare word by word, character by character this, which you claim works,

model5[k2_?NumericQ, k3_?NumericQ, S0_?NumericQ] :=
  (model5[k2, k3, k4, S0] = P[10] /. First[NDSolve[{
        Eu'[t] == k2 ES[t] + k3 ES[t] - k1 Eu[t] S[t] - k4 Eu[t],
        ES'[t] == k1 Eu[t] S[t] - ES[t] (k2 + k3),
        E2'[t] == k4 Eu[t], S'[t] == k2 ES[t] - k1 Eu[t] S[t], P'[t] == k3 ES[t],
        S[0] == S0, Eu[0] == Et, ES[0] == 0, E2[0] == 0, P[0] == 0},
       {Eu, ES, E2, S, P}, {t, 0, 60}]]);
fit = NonlinearModelFit[data, model5[k2, k3, S0],
{{k2, 4000}, {k3, 100}},
S0]

with this, which you claim does not work

Model3[Vmax_?NumericQ, Km_?NumericQ, Fuc_?NumericQ, Pdiff_?NumericQ, Cb0_?NumericQ, te_] :=
  (Model3[Vmax, Km, Fuc, Pdiff, Cb0, te] = Ca[te] /. First[NDSolve[{ 
        Ca'[t] == ((Vmax Cb[t])/(Km + Cb[t]) - Pdiff Fuc Ca[t] + Pdiff Cb[t])/Va, 
        Cb'[t] == (Pdiff Fuc Ca[t] - Pdiff Cb[t] - (Vmax Cb[t])/(Km + Cb[t]))/Vb,
        Ca[0] == 0, Cb[0] == Cb0 },
       {Ca, Cb}, {t, 0, 50}]]);
fit1 = NonlinearModelFit[Dataimp, {Model3[Vmax, Km, Fuc, Pdiff, Cb0, te]},
{{Vmax, 0.001119}, {Km, 15}, {Fuc, 0.1}, {Pdiff, 0.000000345}}, {Cb0, te},
   Weights -> (1/#3 &)]

What is different? Is it absolutely necessary that it be different?

Why is Cb0 and te written differently than P? Why is Ca written differently than P? Are there other differences? That can be made the same?

POSTED BY: Bill Simpson
Posted 10 years ago

HI, Thanks for your efforts. I am attaching the file, where I had used these functions, earlier. They had worked. But I am not able to find the mistake which I am doing in "error.nb" .

Thanks for the help

Attachments:
POSTED BY: Jaydeep Yadav
Posted 10 years ago

I take your latest version of error.nb and evaluate that with no changes.

In part of your last step you wish to plot a function of Ca[te] and this does not work. So the first thing I do when a plot does not work is to look at a table of values of the function and see if this tells me why this does not work.

In[15]:= Table[Ca[te], {te, 0, 5}]

Out[15]= {Ca[0], Ca[1], Ca[2], Ca[3], Ca[4], Ca[5]}

Thus it appears that your Ca function may be undefined. That might explain why it will not plot.

Try a table of fit1[Ca[te], te]]

In[16]:= Table[fit1[Ca[te], te], {te, 0, 5}]

Out[16]= {Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[0], 0],
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[1], 1], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[2], 2], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[3], 3], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[4], 4], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[5], 5]}

and again it appears undefined.

Try a table of the expression you were trying to plot

In[17]:= Table[fit1[Ca[te], {Cb0, 0.1}], {te, 0, 5}]

Out[17]= {Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[0], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[1], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[2], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[3], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[4], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[5], {Cb0, 0.1}]}

and again it appears undefined.

Put the Evaluate back, which is not going to fix this.

In[18]:= Table[Evaluate[fit1[Ca[te], {Cb0, 0.1}], {te, 0, 5}]]

Out[18]= {Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[0], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[1], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[2], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[3], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[4], {Cb0, 0.1}], 
 Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Ca[5], {Cb0, 0.1}]}

and again it appears undefined. But this gives a small red warning ^ which is a sign there is something very wrong with your code.

Look at all details of Cb0 for information

In[19]:= FullForm[Cb0]

Out[19]//FullForm= Cb0

Thus Cb0 appears completely undefined. Thus you are not able to get a graph of Ca[te]. Thus the problem is with your NonlinearModelFit or with your NDSolve.

Read the documentation for FittedModel. See there is an example showing how to use Normal to see information about the result of the fit.

In[20]:= Normal[fit1]

Out[20]= Model3[0.00239865, 11.8628, 0.899403, 2.04314*10^-6, Cb0, te]

See that you do not get back an expression which models your data. Thus it seems likely that your use of NonlinearModel is incorrect and has failed.

I do not believe I understand almost anything about your code. But it looks like you are not asking NonlinearModelFit to find Ca[t], it appears you are asking it to find Cb0. Perhaps Ca[t] is gone long before you are trying to plot it. Since it appears you are asking it to find Cb0 and even Cb0 has no value. From the documentation for FittedModel, if I were going to guess from the examples I see there I would think I might look at the value of fit1[2,3] and not something like fit1[Ca[te],{Cb0, 0.1}].

In[21]:= fit1[2, 3]

Out[21]= 95.5808

and

In[22]:= Table[fit1[2, i], {i, 0, 5}]

Out[22]= {0., 60.2507, 85.3232, 95.5808, 99.7467, 101.433}

but the first argument to fit1 is almost certainly not 2. I have no idea what that value should be. The second I might guess would be a time between 0 and 50 so try

In[23]:= Plot[fit1[2, i], {i, 0, 5}]

Out[23]= ...PlotRemoved...

and at least it shows a plot, but this plot is guaranteed to be incorrect because we do not know what Cb0 should be or what te should be or even what Ca should be.

Can you begin to imagine why this problem seems more and more hopeless? Because it doesn't seem like any substantial progress is being made, even after all the requests to "please fix this.". There just does not seem to be an understanding of how to use NonlinearModelFit in the way you are using it and be able to get a correct and useful answer. That appears to be the fundamental problem which has been unchanged from the very beginning, other than changing random parts of the code which did not seem to help.

I hope it works out for you. Do you know and can you program in FORTRAN? You might consider that.

POSTED BY: Bill Simpson
Posted 10 years ago

Hi, The File that I had attached is a small part of a complete file. While copying I made some errors like I did not give the values of Va and Vb, and for the second error I was trying to change some things so that it works, in that process I made a error in the code. So finally I want to plot a graph of Ca[te] Vs te using the fitted model. I tried doing that, it was showing some error. The error shows that I need to put two arguments, but even after putting two arguments, I am not able to get the curve.

I have attached a new file, with all the corrections that you mentioned. Please let me know the errors if any? Still I am not able to get the graph, there is no error, nut still the the graph do not appear. Please tell me where am I going wrong?

Attachments:
POSTED BY: Jaydeep Yadav
Posted 10 years ago

In your attached error.nb notebook

Error 1: Va and Vb are not assigned numeric values, but you are using those inside NDSolve that that will always fail. It is very unfortunate that an error message from NDSolve did not tell you this in exactly those words.

Error 2: You are solving for Cb0 and te in your NonlinearModelFit and you will then need to evaluate the resulting fit1 with two arguments, but you are only giving it one argument in fit1[Ca[te]]. The error message tells you exactly this same thing. After many other changes to try to get NonlinearModelFit to produce any kind of result without failure I was able to get fit1[1,1] to provide one result without errors, but that was not even attempting to plot fit1[1,t].

But these are the smallest and most obvious errors of what is probably a forest of other problems and errors and misunderstandings.

I cannot know, but it appears from the way that parts of this are written that you are randomly inserting things that you have seen elsewhere and hoping that they have something to do with your problem.

Example from your code

Model3[Vmax_?NumericQ, Km_?NumericQ, Fuc_?NumericQ, Pdiff_?NumericQ, Cb0_?NumericQ, te_] :=
  Model3[Vmax, Km, Fuc, Pdiff, Cb0] = ...

I believe you have seen some code from somewhere use this and so you think this must necessary and so you insert it in your code. But the number of arguments is not the same in those two Model3. There is a purpose in using this method of writing functions, but I believe this is probably the least of all the problems you have now and is not doing anything correct in your code.

You may not have begun by first learning a considerable amount about Mathematica programming and then started this project by writing the smallest bits of code, testing that to make certain that you really understand exactly how it works and that it is correct, then adding the smallest possible additional bit of code to get slightly closer to your entire problem and repeating the understanding and testing process over and over and over and over until you get to your final result.

I attempted to do some of that process by making many changes and fumbling around and was able to figure out and I believe correct some things in the code to try to get small parts of this to work, but I cannot begin to guess how to tell you how to "fix your problem."

Do you possibly have any other tools that you could start over with and solve this problem using them? That might be easier and faster.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract