Troubleshooting ODE45 Matlab Subfunction: Save Matrix Values in Main Code

  • Context: MATLAB 
  • Thread starter Thread starter hasek
  • Start date Start date
  • Tags Tags
    Matlab Ode45
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting the use of the ODE45 function in Matlab, specifically focusing on saving matrix values generated within a subfunction during the integration process. Participants explore methods for indexing and storing results across iterations, as well as concerns about the impact of these methods on simulation performance.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to save values of two matrices, vsd and vsq, generated within the ODE subfunction but struggles with indexing due to the need for initialization at each iteration.
  • Another participant suggests implementing a custom fixed step-size Runge-Kutta (RK4) method as an alternative to ODE45.
  • A participant shares their approach of using a load and save mechanism to maintain an index variable across function calls, questioning whether this could negatively affect simulation time or result in incorrect values.
  • A different participant seeks assistance with saving and plotting multiple outputs from a loop using ODE45, asking how to retain values of t and h across iterations for later plotting.

Areas of Agreement / Disagreement

There is no consensus on the best method for saving values from the ODE45 subfunction, as participants propose different approaches and express concerns about performance implications. The discussion remains unresolved regarding the optimal solution.

Contextual Notes

Participants express uncertainty about the implications of their proposed solutions on simulation accuracy and performance. There are also limitations related to the specific implementation details of saving and loading variables in Matlab.

Who May Find This Useful

Individuals working with Matlab's ODE45 function, particularly those interested in saving intermediate results during numerical simulations or seeking alternatives to built-in functions.

hasek
Messages
3
Reaction score
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
You could always write you own one-line (fixed step-size) RK4 code.
 
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
 
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..
 

Similar threads

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