MATLAB: Ode solver varying parameters

ode45parameters

Hello,
I have a three differnetial equations with 8 parameters in total of which 1 parameter p7 actually changes with time (p7=a*b^y*c^z – given for representative purpose, but i have a similar equation for that). How do i solve this with ode45? When i tried i got the following error: Unable to perform assignment because the left and right sides have a different number of elements. This is because the p7 has several values wheras other parametrs have only 1. Can someone help me how to solve ode with changing parameters.
CODE:
dx = zeros(3,1);
p7=a*b^y*c^z;
dx(1)=((p1.*((x(2)/(x(2)+p2)).*(x(3)/(x(3)+p3)))).*x(1));
dx(2)=((-((1/p4).*(p1.*((x(2)/(x(2)+p2)).*(x(3)/(x(3)+p3)))))+p5).*x(1));
dx(3)=(((-((1/p6).*(p1.*((x(2)/(x(2)+p2)).*(x(3)/(x(3)+p3)))))+p5).*x(1))+(p7.*(p8-x(3))));

Best Answer

You do not assign multiple values to p7. Instead, each time that p7 needs to change values in a discontinuous way, you need to terminate the ode45, and then restart it from where you left off except with the new p7 value.
If the p7 changes happen at known times, then the way to do this is to call ode45 with tspan reflecting the interval in which p7 is to remain constant. For example,
p7times = [0, 1, 2.5, 5, 5.2, 10];
p7vals = [pi/9, 0.3, exp(-1), sqrt(2), -3]; %one fewer
num_interval = length(p7vals);
x0 = [0 0 0];
t = cell(num_interval,1);
x = cell(num_interval, 1);
for idx = 1 : num_interval
p7 = p7vals(idx);
[t{idx},x{idx}] = ode45(@(t,x) try_eqns(t, x, p7), p7times(idx:idx+1), x0);
x0 = x{idx}(end,:);
end
If discontinuous changes to your p7 depend upon the results of the ode, then you should instead use ode event functions to detect the situation and terminate the integration and then resume with the new p7. There is a useful example of this: see ballode