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

In summary, the programmer is looking for a way to save the value of a function at each time step, but is unsuccessful.
  • #1
t-gas
3
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
  • #2
Use an array of points for tspan rather than just the start and end points. For example, tspan = 0:0.05:0.8;
 
  • #3
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)
%%%%%%%%%%%%%%%%
 
  • #4
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:
  • #5
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)];
%%%%%%%%%%%%%%%%%
 
  • #6
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.
 

What is ODE45 in Matlab?

ODE45 is a function in Matlab that solves ordinary differential equations (ODEs) with a specified initial value. It uses a combination of fourth and fifth-order Runge-Kutta methods to provide accurate solutions.

How do I use ODE45 to follow a function's value?

To use ODE45 to follow a function's value, you need to specify the function, initial conditions, and the range of values over which you want to follow the function. You can also specify the desired accuracy and other options in the ODE45 function call.

What is the output of ODE45?

The output of ODE45 is a vector of values that represent the solution to the specified ODE at each time step. This output can be plotted or used for further analysis.

Can ODE45 handle systems of differential equations?

Yes, ODE45 is capable of solving systems of differential equations. You can specify multiple functions and initial conditions in the ODE45 call to solve a system of equations simultaneously.

What are the advantages of using ODE45?

ODE45 is a robust and efficient function for solving ODEs. It can handle a wide range of problems and provides accurate solutions. It also allows for easy visualization and analysis of the results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top