I have here a little program that I do not know how to tackle, I have previously written a code that would solve for me a system of two differential equations which works fine.
However, I now have to solve a system of three differential equations:
d(alphai)/dt = ui = k * alphai*f(tethai)
d(tethai)/dt = alphai*f(tethai) - (tethai - tethaex)
d(tethaex)/dt = sigma*(Sum(1.N)(tethai - tethaex)) - phi*tethaex
Here are my codes so far but I get some errors:
%Script for differential derivatives specifcation
function dy = NRateDE(T,Y,p)
%Extract parameters from P
u0 = p(1);
g = p(2);
k = p(3);
a = p(4);
b = p(5);
N = p(6);
%Format output as a column vector
dy = zeros(3,1);
%Set differential equations
dy(1) = (u0*exp(-g*T)) - (k*Y(1)*exp(Y(2)));
dy(2) = Y(1)*exp(Y(2)) - (Y(2)) - Y(3);
A = Sum(Y,N);
dy(3) = (a .* A) - (b .* Y(3));
end
function A = Sum(Y,N)
for i=1:N
A = A + (Y(2) - Y(3));
end
end
And finally here is the main function:
%% Initialize the environment
close all;
clear all;
clc;
%%Define parameters and initial conditions
u0 = 0.12; g = 0; k = 0.09; N = 2; a = 0; b = 0.1; z = 0;A=0; %Parameters
MaxTime = 300;
T = 0:0.01:MaxTime; %integration time
Yinitial = 0; Yminitial = 0; %intial conditions
%%Solve and generate equations
%#Numerical approximation
for z=1:N
%Create random numbers to change the value of u0
a1 = - 0.01;
b1 = 0.01;
r = (b1-a1).*rand(1) + a1;
u0 = u0 +r; %value changes on each iteration
[T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial],[],[u0 g k a b N A]);
end
%#Analytical approximation
tau = 0:0.2:MaxTime;
tetha = (u0*exp(-g*tau))/k;
alpha = u0*exp(-g*tau).*exp(tetha);
With N = 2, I should get two set of matrices of [T,Y] but I have no clue of how should I correct the code. I am fairly new to Matlab so please forgive any silly mistake haha'.
Any idea, hint?
Thank you for your help in advance!
EDIT: I Have slightly changed the first function and the "RateDE" was not intentional. Also here is the error message:
Attempted to access Y(3); index out of bounds because numel(Y)=2.
Error in NRateDE (line 18) dy(2) = Y(1)*exp(Y(2)) - (Y(2)) - Y(3);
Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 149) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in RunRateNparticles (line 20) [T,Y] = ode15s(@NRateDE,[T],[Yminitial Yinitial],[],[u0 gkab NA]);
Right!
I understand the error messages and have corrected them by adding an element on Y:
[T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial Ylinitial],[],[u0 g k a b N A]);
However, I am still unable to solve the set of three differential equations given on the first message.
Here are the current error messages:
Undefined function or variable "A".
Error in NRateDE (line 19) A = A + (Y(2) - Y(3));
Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23s (line 120) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in RunRateNparticles (line 20) [T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial Ylinitial],[],[u0 gkab NA]);
The solution to the original problem is outlined in your own answer already.
Y
needs to have 3 elements, so you can just add another initial condition.
The new problem is a missing assignment. The Sum
function tries to add something to A
, but this variable doesn't exist yet. Fixing your function like this should work:
function A = Sum(Y,N)
A = 0;
for i=1:N
A = A + (Y(2) - Y(3));
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.