How to follow a function's value in ODE45 Matlab?

  • Context: MATLAB 
  • Thread starter Thread starter t-gas
  • Start date Start date
  • Tags Tags
    Matlab Ode45 Value
Click For Summary

Discussion Overview

The discussion revolves around how to extract and plot the values of a function defined within an ODE45 integration in MATLAB. Participants are exploring methods to output the values of the variable 'm=2/y(1)' at each time step during the integration process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks a way to plot or output the values of 'm' at each time step during the ODE45 integration, noting that MATLAB only returns the final value.
  • Another participant suggests using an array of points for 'tspan' to potentially capture more data points.
  • A different participant mentions that even with an array for 'tspan', they are unable to retrieve the values of 'm' at each time step and speculates that additional functions like "odeset" might be necessary.
  • One suggestion involves creating a data file using MATLAB's file handling functions to store numerical calculations, including the values of 'm'.
  • Another participant expresses frustration that they cannot output the values of 'm' despite using file writing commands, indicating that 'm' is calculated internally and not accessible for output.
  • One participant proposes that the issue may lie in how the output file is being generated, suggesting that MATLAB's handling of the ODE45 output might be affecting the ability to write 'm' values correctly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a solution. Multiple competing views and approaches are presented, and the discussion remains unresolved regarding the best method to output the values of 'm' during the ODE integration.

Contextual Notes

Participants mention potential limitations in MATLAB's handling of internal calculations and the accessibility of intermediate values during ODE integration. There are unresolved questions about the correct implementation of file writing commands and the structure of the output data.

t-gas
Messages
3
Reaction score
0
Hi everyone,
It is extremely helpful for me if you could answer the following question. I hope it is a relatively simple problem:

I just need a plot or an output data file of the values of the function 'm=2/y(1)' appearing in the function 'myfun' for each time-step. This is extremely easy in other programming languanges but in Matlab when I plot(t,m) it only returns the value at the end of the integration.


%%%%%%%%%%
function manoura;
tspan = [0 0.8];
y0 = [3; 7/3];

[T, Y] = ode45(@myfun, tspan, y0);
subplot(211); plot(T,Y(:,1));
subplot(212); plot(T,Y(:,2));

function F1 = myfun(t,y);
m=2/y(1);
F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];
%%%%%%%%%

Thank you in advance!
I have tried everywhere and I cannot find a solution.
Thank you in advance!

(I have already posted it as "How to follow a function's value in ODE45 Matlab?")
 
Physics news on Phys.org
Use an array of points for tspan rather than just the start and end points. For example, tspan = 0:0.05:0.8;
 
I have tried an array but it does not still show the values for (m) at each time step. Since my actual code is more complicated and it runs for about more than 1000secs I guess there must be also another solution to that.

Perhaps it needs "odeset" or "handle" functions, I do not really know!


Thanx anyway!

My code now using the array for tspan has became:
%%%%%%%%%%%%%%%
function manoura;
tspan = 0:0.2:0.3:0.4:0.6:0.8:0.9;
y0 = [3; 7/3];

[T, Y] = ode45(@myfun, tspan, y0);

function F1 = myfun(t,y);
m=2/y(1);
F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];
plot(t,m)
%%%%%%%%%%%%%%%%
 
Why don't you create a data file?

Google for fopen, fclose, and fprint commands. With those you can create a data archive containig all numerical calculations.

If you write
Code:
x=fopen('nameofarchive.dat','w')
Matlab will create an archive and asign it the value x. The 'w' stands for write.

Then with fprint, Matlab will save the data in the archive (browse the help for specific commands)
Code:
fprint(x,'%f  %f \n', var1,var2)
where var1, var2 are the variables you want to save. The %f %f \n tells MATLAB the way you want the archive to be formatted (again, check the help).

Finally when you are done, type
Code:
fclose(x)
to close the archive.

If you latter on whant to call the data archive, simply use the code
Code:
[v1,v2]=textread('nameofarchive.dat','%f %f',m)
and then you can use v1 and v2 for further calculations. Here, m is the number of columns of your archive (you can modify it to read it partially I believe).

It's a great idea to create data archives instead of carrying variables in order to free up memory for Matlab, as it turns out to be quite stupid in allocating memory blocks by itself. This will improve considerably the calculation time of your code. Also remeber to clear as many variables as you can when no longer need them, for example, after you finish writting your archive, clear var1, var2 with the line
Code:
clear(var1,var2)

Anyway, you can find all sorts of neat tricks in the web, just look for them.
 
Last edited:
Thanx, I have included "fprinf" commands and etc... but still no luck!

I can write an output file for all except the one I want, "m" :(
I guess it calculates "m" time series internally whithin the ode45 and keeps no records for output.
When I include the "fprinf" and so in function "manoura", it does not recognize "m".
On the other hand when I include the "fprinf" and so in function "myfun", it calculates again only the last value of t.

My file is shown below:
%%%%%%%%%%%%%%
function manoura
tspan = [0 0.8];
y0 = [3; 7/3];

[T, Y] = ode45(@myfun,tspan, y0);
plot(T,Y(:,1),T,Y(:,2))

fid=fopen('mvalues.txt', 'w+');
for c = 1:size(T)
fprintf(fid, '%5f %5f %5f\n', [T(c); Y(c,2); Y(c,1)]);
end
fclose(fid);

function F1 = myfun(t,y)
m=2/y(1);
F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];
%%%%%%%%%%%%%%%%%
 
I believe is in the way you are asking the program to write the file. When Matlab performs ode45, it generates an array of points, and I think what you are doing is asking it to create the file by steps. I am not sure about this, better go to a Matlab forum and ask there.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K