Troubleshooting ODE45 Matlab Subfunction: Save Matrix Values in Main Code

In summary, the conversation revolves around a problem with the subfunction ODE45 in Matlab. The main issue is how to save the values of two matrices created within the subfunction. One solution suggested is to create an index inside the subfunction, but there is concern about how it may affect the simulation time and if it will provide accurate values. Another option mentioned is to write a custom one-line RK4 code. In addition, there is a question about how to save multiple values of [t,h] in a loop and plot them.
  • #1
hasek
3
0
I'm stuck with a problem with the subfunction ODE45 of Matlab:

I need to save two matrixes' values created inside the ODE subfunction.
I can't create an index inside the subfunction because I have to define it at the start so at every round it returns to zero.

This is what I've done in the main:

t0 = 0;
tf = 450;
y0 = [0 0 0 0 0 0];
options = odeset('RelTol', 1e-6);
[t, y] = ode45('ac_im3_IPM_prova1', [t0,tf] , y0, options);

and this is the subfunction:

function yp = ac_im3_IPM_prova1(t, y);
.
.
.
vsd = 2/3 * (vsa * cos(ttg) + vsb * cos(ttg - 2*pi/3) + vsc * cos(ttg - 4*pi/3));
vsq = -2/3 * (vsa * sin(ttg) + vsb * sin(ttg - 2*pi/3) + vsc * sin(ttg - 4*pi/3));
.
.
.
yp(1) = (vsd - Rs*isd + (wge)*fsq);
yp(2) = (vsq - Rs*isq - (wge)*fsd);
yp(3) = ( - Rr*ird + (wge-p*wm)*frq);
yp(4) = ( - Rr*irq - (wge-p*wm)*frd);
yp(5) = (tm - (TL + KD*wm)) / J;
yp(6) = wge;

yp = yp';

I'd like to save vsd and vsq values of every round in a matrix or in someway!
 
Physics news on Phys.org
  • #2
You could always write you own one-line (fixed step-size) RK4 code.
 
  • #3
Ok, now I created an index inside the subfunction in this way:

i=0;
if t ~= 0
load('i');
end
i=i+1;
save('i');

Now there is another problem:

I'm wondering if this procedure could affects the simulation time returning me wrong values
 
  • #4
Hey i need help in ODE45 too

i have the following code

>>for i=1:5
>>tspan=[0 30]
>>h0=Hv(i)
>>[t,h]=ode45['a ODEfunction',tspan,h0]
>>plot(t,h)
>>end

in this loop how can i save 5 values of [t,h],
as t, h produces each loop run like follows
t=
1
2
3
4
.
.
.
h=
5
6
7
8..

also i need to plot all of them n i could view the 5 graphs them..pls help..
 
  • #5


I would recommend using the "OutputFcn" option in the ode45 function to save the values of vsd and vsq at each time step. This option allows you to specify a function that will be called at each time step and can be used to save any desired variables.

For example, you could create a new function, let's call it "save_values", that takes in the current time and state variables (vsd and vsq) and stores them in a matrix. Then, in your main code, you can use the "OutputFcn" option to call this function at each time step and save the values.

Another option could be to use global variables to store the values of vsd and vsq within the subfunction and then access them in the main code after the ode45 function has finished running. However, this approach is not recommended as it can lead to potential errors and is not considered good programming practice.

I hope this helps you in troubleshooting your issue with saving the matrix values in your main code. It's important to carefully consider your options and choose the most appropriate solution for your specific problem.
 

What is ODE45 in Matlab?

ODE45 is a built-in function in Matlab that solves ordinary differential equations (ODEs) using a Runge-Kutta method. It is commonly used in various scientific and engineering applications.

How can I troubleshoot issues with ODE45 in Matlab?

If you encounter problems while using ODE45, there are a few steps you can take to troubleshoot. First, check that your initial conditions and equations are specified correctly. You can also try adjusting the tolerances and time step options. Additionally, make sure your inputs and outputs are in the correct format.

What are subfunctions in Matlab?

Subfunctions in Matlab are functions that are defined within a main function and can only be accessed by that main function. They are useful for organizing and modularizing code, as well as avoiding naming conflicts.

How can I save matrix values from a subfunction to the main code in Matlab?

To save matrix values from a subfunction to the main code in Matlab, you can use the "global" keyword to make the matrix a global variable. This will allow the subfunction to access and modify the matrix, and any changes made will be reflected in the main code. Alternatively, you can use the "return" statement to output the matrix from the subfunction and assign it to a variable in the main code.

Can I use ODE45 in a loop in Matlab?

Yes, you can use ODE45 in a loop in Matlab. This can be useful for solving ODEs with changing parameters or initial conditions. However, be careful to not overwrite your results or use too small of a time step, as this can lead to errors or excessive computation time.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
Back
Top