Message Boards Message Boards

2
|
10845 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Getting Value from Interpolating Function

I solved a system of ordinary differental equations using NDsolve. Their plots look perfect. However, I am unable to extract the values from them at particular values.

sol = NDSolve[{eqR1, eqR2, icR1, icR2, icm}, {R[t], m[t]}, {t, 0, 30*10^(-5)}]
Rbubble = R[t] /. sol
mdot = m[t] /. sol
Plot[{Rbubble/Ro}, {t, 0, 30*10^(-5)}, PlotRange -> Automatic, 
 AxesLabel -> {"Time (in s)", "Bubble Radius"}]
Plot[mdot*4*Pi*Rbubble^2, {t, 0, 30*10^(-5)}, PlotRange -> Automatic, 
 AxesLabel -> {"Time (in s)", "Mass Flux at interface"}]

I am sure the solutions are correct because the plots look right. But I am unable to access Rbubble[0].

Please help!!!

POSTED BY: Raunak Bardia
4 Replies
Posted 9 years ago

After the last Plot statement you are missing a semicolon. This means the Plot and checktemp are multiplied, so for that expression the head is Times, so you are trying to set that expression to a number, rather than the symbol checktemp. This may work:

For[i = 1, i <= 1, i++, 
 sol = NDSolve[{eqR1, eqR2, R[(i - 1)*dt] == Last[Ro], 
    R'[(i - 1)*dt] == Last[Rpo], m[(i - 1)*dt] == Last[mo]}, {R[t], 
    m[t]}, {t, (i - 1)*dt, i*dt}];
 Rbubble[t_] = R[t] /. sol;
 Plot[Rbubble[t], {t, (i - 1)*dt, i*dt}] BubbleRate[t_] = Rbubble'[t];
 Plot[BubbleRate[t], {t, (i - 1)*dt, i*dt}] mdot[t_] = m[t] /. sol; 
 checktemp = Last[Rbubble[(i)*dt]];
 Plot[mdot[t], {t, (i - 1)*dt, i*dt}] ]

Note that I moved the assignment to before the Plot statement, so the Plot appears without a terminating semicolon. A semicolon suppresses output. This means the earlier Plot statements generate no output. If you want to see the plots you can save them as variables and use Show later, or you could enclose them in Print[] for immediate display.

Best, David

POSTED BY: David Keith
Posted 9 years ago

Hi Raunak,

It would be easier to respond if you had included the definitions for the equations. Then I could reproduce the work and display a syntax for doing what you want.

But ---- Rhubble=R[t]/.sol produces an expression in t. When you plot that, Plot substitutes values for t and the plot works fine. But Rhubble is not a function; it is an expression. To evaluate it at t=0, you could use Rhubble/.t->0, which makes the replacement for t.

Alternatively, you could make Rhubble a function by using Rhubble[t_]=R[t]/.sol. Now you would need to plot Rhubble[t], and Rhubble[0] would return a value.

BTW -- I have used the variable names you gave. But it is better to begin user symbols with lower case. This is because Mathematica uses upper case for reserved symbols.

Best,

David

POSTED BY: David Keith

Hi David,

Thank you for your help. I understood the issue and corrected it appropriately in my code. It works fine although this has led me to a different problem.

Question - Can we reassign a variable in a loop?

In[2595]:= For[i = 1, i <= 1, i++, 
 sol = NDSolve[{eqR1, eqR2, R[(i - 1)*dt] == Last[Ro], 
    R'[(i - 1)*dt] == Last[Rpo], m[(i - 1)*dt] == Last[mo]}, {R[t], 
    m[t]}, {t, (i - 1)*dt, i*dt}];
 Rbubble[t_] = R[t] /. sol;
 Plot[Rbubble[t], {t, (i - 1)*dt, i*dt}]
  BubbleRate[t_] = Rbubble'[t];
 Plot[BubbleRate[t], {t, (i - 1)*dt, i*dt}]
  mdot[t_] = m[t] /. sol;
 Plot[mdot[t], {t, (i - 1)*dt, i*dt}]
   checktemp = Last[Rbubble[(i)*dt]]
 ]
checktemp = Last[Rbubble[(i - 1)*dt]]

During evaluation of In[2595]:= Set::write: Tag Times in 0  is Protected. >>

Out[2596]= 0.0000500319

The assignment of checktemp inside the loop gives an error, however, a similar assignment outside the loop works fine. How should I overcome this?

Question 2: In the code above, I am extracting the value from function Rbubble using "Last". Is there a better way to get the value as an output rather than a list?

Please help!

Thanks.

P.S. - I am not sure if my work allows me to share those equations hence, I just uploaded the code essential to the issue. Sorry for the inconvenience.

POSTED BY: Raunak Bardia
Posted 9 years ago

Regarding question 2, you will notice that the solution sol for NDSolve is a rule in a list. Most solve functions do this to accomodate multiple solutions. When defining the function you can use

Rbubble[t_] = R[t] /. Last[sol];

Or

Rbubble[t_] = R[t] /. First[sol];

Or

Rbubble[t_] = R[t] /. sol[[n]];

to pick out a particular rule in the solution set. Now your function will return a number rather than a list.

POSTED BY: David Keith
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