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.
采纳的回答
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 个评论
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.
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.
@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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
