Need help in solving diffrential equation with initial conditions. Kindly help me correct my code.

1 view (last 30 days)
My objective is to solve for X_1 and X_2 with inital conditon x1(0) and x2(0) for time t=0 to 1000
syms x1(t) syms x2(t)
%constants
k1=0.015;k2=0.025;k3=0.038;
alpha=0;k=7;a=2;
%Initial Conditions
x1(0)=0.45+0.05*alpha;
x2(0)=0.65-0.15*alpha;
t=0:1000;
K_lower=k1+alpha(k2-k1);
K_upper=k3-alpha(k3-k2);
X_1=dsolve(diff(x1,t)==(K_lower)*(x1)*(1-(x2/k))*(x1-a),x1(0)==0.45+0.05*alpha);
X_2=dsolve(diff(x2,t)==(K_upper)*(x2)*(1-(x1/k))*(x2-a),x2(0)==0.65-0.15*alpha);
Error using sym/subsasgn (line 951)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be
symbolic variables, and function body must be sym expression.
Error in Initial_Conditions_alpha (line 4)
x1(0)=0.45+0.05*alpha;

Accepted Answer

Ameer Hamza
Ameer Hamza on 16 Mar 2020
You are defining the initial condition incorrectly. Check this code
syms x1(t)
syms x2(t)
%constants
k1=0.015;k2=0.025;k3=0.038;
alpha=0;k=7;a=2;
K_lower=k1+alpha*(k2-k1);
K_upper=k3-alpha*(k3-k2);
Eq = [diff(x1,t)==(K_lower)*(x1)*(1-(x2/k))*(x1-a);
diff(x2,t)==(K_upper)*(x2)*(1-(x1/k))*(x2-a)];
%Initial Conditions
conditions = [x1(0)==0.45+0.05*alpha;
x2(0)==0.65-0.15*alpha];
X_1 = dsolve(Eq, conditions);
However, dsolve is used to find an analytical solution. It will return an empty vector because an analytical solution of your system does not exist. You need a numerical solver like ode45. Therefore, try this code next
k1=0.015;k2=0.025;k3=0.038;
alpha=0;k=7;a=2;
K_lower=k1+alpha*(k2-k1);
K_upper=k3-alpha*(k3-k2);
odefun = @(x) [(K_lower)*x(1)*(1-(x(2)/k))*(x(2)-a); ...
(K_upper)*x(2)*(1-(x(1)/k))*(x(2)-a)];
tspan = [0 10];
conditions = [0.45+0.05*alpha; ...
0.65-0.15*alpha];
[t, x] = ode45(@(t,x) odefun(x), tspan, conditions);
plot(t, y);

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!