Solving dependent functions using ODE45

Hi All,
I am trying to solve a set of equations that are dependent on each other. dx_dt(1) is dependent on dx_dt(2). dx_dt(2) and dx_dt(3) are dependent on dx_dt(1). I have not been able to solve this. Any suggestions are greatly appreciated.

 채택된 답변

Star Strider
Star Strider 2016년 6월 24일

0 개 추천

The ode45 and other functions (choosing the appropriate solver is important) are able to solve such systems of differential equations relatively easily. Please post your code for your differential equation funciton, and original equations if necessary.

댓글 수: 5

Josh
Josh 2016년 6월 27일
편집: Josh 2016년 6월 27일
Thanks for the help. My ODE code is below. When I run the original code, I get an error in my dynamics file. The error is "Undefined function 'vLp1'".
*Note* vLp1 = dx_dt(2,:) vL = dx_dt(1,:)
function dx_dt = dyn(t,x,params)
global Vs D fsw L C C2 Rload rL Lp1 Lp2 Cj1 Cj2;
iL = x(1);
iLp1 = x(2);
iLp2 = x(3);
vC = x(4);
vj1 = x(5);
vj2 = x(6);
dtri = x(7);
d = D;
tri = dtri + 0.5;
if (d >= tri)
x = 0;
y = 1;
else
x = 1;
y = 0;
end
vrL = rL*iL;
iLoad = vC/Rload;
dx_dt(1,:) = (x*(Vs - vC) + y*(-vj1 - vLp1 - vC))/L;
dx_dt(2,:) = (x*(-Vs - vj1) + y*(-vj1 - vL - vC))/Lp1;
dx_dt(3,:) = (x*(-Vs - vj2) + y*(-vj2 - vL -vC))/Lp2;
dx_dt(4,:) = (iL - iLoad)/C;
dx_dt(5,:) = (iLp1 - iD1)/Cj1;
dx_dt(6,:) = (iLp2 - iD2)/Cj2;
dx_dt(7,:) = 2*fsw*sign(cos(2*pi*fsw*t));
My pleasure.
Do not use globals! They cause the problems you’re seeing, because you cannot control them and they make debugging your code almost impossible. I don’t know what the ‘params’ argument refers to, since you never use it in your code.
I would do this instead:
function dx_dt = dyn(t,x, Vs, D, fsw, L, C, C2, Rload, rL, Lp1, Lp2, Cj1, Cj2)
and delete the global call.
I don’t understand what you’re doing in the if block, but be aware that the ODE solvers do not handle integrating across discontinuities at all well (No numerical solvers do.) The same caution applies to:
dx_dt(7,:) = 2*fsw*sign(cos(2*pi*fsw*t));
although if the magnitude of ‘dx_dt(7,:)’ isn’t large, especially with respect to the other values, the effect of the discontinuity may not be significant.
Josh
Josh 2016년 6월 28일
편집: Josh 2016년 7월 1일
I thought I was being clever using globals. I'll scratch that off of my to-do list and stay away from them. I've added them like you suggested above.
I am still having the same problem though. I changed my code as shown below. Looking at the previous code I posted, vLp1 definitely wasn't defined. I noted what it was in my post, but it was not in my actual code. Below is what I have now. I am getting the same error, "Undefined function or variable dxdt".
Also, with the sign function, I'm creating a triangle wave. Basically, I'm doing PWM with a triangle wave with magnitude 1.
The use of global variables is a remnant of earlier computer languages (like FORTRAN II that I learned programming with back in 1968) when that was the only way to pass variables and avoid some restrictions on subroutine (MATLAB function) usage. They are vestigial, and will eventually disappear. They are definitely to be avoided in MATLAB code.
One other problem you mentioned in a subsequent Question was that ‘dxdt(1)’ requires ‘dxdt(2)’. I would eliminate the double subscripts (that could be part of the problem), and instead insert a line defining ‘dxdt’ as a column vector from the outset:
...
iLoad = vC/Rload;
dxdt = zeros(7,1);
dxdt(1) = (x*(Vs - vC) + y*(-vj1 - Lp1*dxdt(2) - vC))/L; % diL/dt
...
and so for the others. I’m not certain that will solve your problem, since I didn’t run your code, but it should at least eliminate the problem you mentioned.
Torsten
Torsten 2016년 6월 29일
@Josh:
Take the first and the second equation (dxdt(1,:) = ... and
dxdt(2,:) = ...) and solve for dxdt(1) and dxdt(2) (2 linear equations in two unknowns).
Alternatively, use the mass-matrix option of the ODE integrators.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2016년 6월 24일

편집:

2016년 7월 1일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by