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
Torsten 2019 年 3 月 6 日
What's the problem with the code from above ? Looks ok to me.
mjki4hj3
mjki4hj3 2019 年 3 月 6 日
I would like for yprime to change from its inital value of 0.4971 (apologies forgot to mention the specific value above) to the final value of yprime which should be reached when y(3) initial 0.3 (third element in bc) is equal to 0.
bc = [1 0.2050 0.3 0.1468]
I'm not sure how to specify this condition in the code I suspect that it requires some kind of loop.
Torsten
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).
mjki4hj3
mjki4hj3 2019 年 3 月 6 日
編集済み: mjki4hj3 2019 年 3 月 6 日
What does it use for the first value of yprime ? Because I havent specified that anywhere and how does it know when to stop ?
Torsten
Torsten 2019 年 3 月 6 日
編集済み: Torsten 2019 年 3 月 6 日
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:
mjki4hj3
mjki4hj3 2019 年 3 月 6 日
Thank you, this clears up my confusion.
khairul anwar
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
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
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

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 3 月 6 日

コメント済み:

2021 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by