Info

This question is closed. Reopen it to edit or answer.

Ode45 data keeps refreshing

1 view (last 30 days)
Lazaros Christoforidis
Lazaros Christoforidis on 24 Apr 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello, I would appreciate if someone help me.
My problem is that I have 5 polyonimals and I made them switch when the value reaches umax it switch to the next one, but I want to stay there or move forwand to the next one polyonimal.
I did the following code, the problem is ode calls this function at each loop and u1max,u2max,u3max,u4max,u5max keeps getting refreshing.
function udot = ok3(t,u,p1,p2,p3,p4,p5,m,maxu,maxT,u1max,u2max,u3max,u4max,u5max)
syms z p(z)
T=symsum(p(m+1-z)*(u(1))^z,z,0,m);
if u(1)<=u1max
for y=1:m+1
T=subs(T,p(y),p1(y));
end
elseif u(1)<=u2max
u1max=5000;
for y=1:m+1
T=subs(T,p(y),p2(y));
end
elseif u(1)<=u3max
u1max=5000; u2max=5000;
for y=1:m+1
T=subs(T,p(y),p3(y));
end
elseif u(1)<=u4max
u1max=5000; u2max=5000; u3max=5000
for y=1:m+1
T=subs(T,p(y),p4(y));
end
elseif u(1)<=u5max
u1max=5000; u2max=5000; u3max=5000; u4max=5000;
for y=1:m+1
T=subs(T,p(y),p5(y));
end
else
T=maxT;
u(1)=maxu;
end
.
.
.
.
udot=double([SM1;SF]);
end

Answers (1)

darova
darova on 24 Apr 2020
function udot = ok3(t,u,P,m,maxu,maxT,umax)
syms z p(z)
T=symsum(p(m+1-z)*(u(1))^z,z,0,m);
persistent umaxc
if isempty(umaxc)
umaxc = u(1);
end
umaxc = max(u(1),umaxc);
if any(umaxc <= umax)
k = find(umaxc<=umax,1,'first');
pp = P{k};
%code
else
T=maxT;
u(1)=maxu;
end
end
Your main script
clear function % clear persistent variable
P = {p1 p2 p3 p4 p5};
umax = [u1max u2max u3max u4max u5max];

Tags

Community Treasure Hunt

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

Start Hunting!