How can I update a variable within an ODE function?
显示 更早的评论
I want to update the value of yprime within the ODE function 'odes'
Function call:
bc = [1 0.2050 0.3 0.1468];
options = bvpset('RelTol', 1e-07);
[z,y] = ode15s(@odes, [0 10], bc, options);
plot(z,y)
Inside ODE function:
function f =odes(z,y)
%parameters
K1 = 8.8721e-7; K2 = -1.4715e-03; alpha = 5.4;
%alegbraic equation relying on solving of ODEs
yprime = (1+(alpha-1)*(y(2)+y(4))-((1+(alpha-1)*(y(2)+y(4)))^2-4*alpha*(alpha-1)*y(4)*y(2))...
^0.5)/(2*(alpha-1)*y(4))
%system of ODEs
f(1) = -K1*(alpha*(1-y(2))*(y(2)-y(4)*yprime)-y(2)*((1-y(2))-y(4)*(1-yprime)));
f(2) = -K1*(alpha*(1-y(2))*(y(2)-y(4)*yprime)-y(2)*((1-y(2))-y(4)*(1-yprime)));
f(3) = (-K1*(alpha*(1-y(2))*(y(2)-y(4))*yprime)-y(2)*((1-y(2))-y(4)*(1-yprime)))/y(1);
f(4) = K2*y(3)/y(4);
end
I have a system of ODEs and one algebraic equation that relies on the output of the ODE. I have an initial value yprime but I'm not sure how to incoporate this into the above expression.
7 个评论
Torsten
2019-3-6
What's the problem with the code from above ? Looks ok to me.
mjki4hj3
2019-3-6
Torsten
2019-3-6
But you have a formula for yprime - so it will change automatically with time according to this formula depending on y(2) and y(4).
Insert the initial values for y(2) and y(4) you specified in the formula for yprime and you'll see which value is used first.
If you want the simulation to stop when y(3)=0, use the event facility of the ODE-integrators:
https://www.mathworks.com/examples/matlab/mw/matlab-ex84325677-simple-event-location-a-bouncing-ball
mjki4hj3
2019-3-6
khairul anwar
2021-11-14
I have the same problem where i need to put all the different result from different values of alpha in one figure. Can anybody help me..I really appreciate that
回答(1 个)
Ricardo Erazo
2019-3-6
0 个投票
These do not answer the question. How can someone update the variable insid the function?
I have a 5 variable system, I need to update the value of y(5) whenever y(1) crosses a threshold. The best solution thus far is to stop integration, reset value outside of the function, and then call the function again. Are there any more efficient methods?
1 个评论
Torsten
2019-3-7
No. Using MATLAB's event detection is the best way to go.
For an example, take a look at
https://www.mathworks.com/examples/matlab/mw/matlab-ex84325677-simple-event-location-a-bouncing-ball
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!