# I want to correct this command please

1 view (last 30 days)
Edited: Fangjun Jiang on 10 Mar 2023
I have model consist of 3 equations and i try to plot it every equation alone with t time so i made the 3 commands one of them accepted by Matlab , and 2 just gave me error and don't know why, anyhone help here please
the system is
Eqn1 = diff(x(t), t) == p*x*(1-y/k)-a1*x*y;
Eqn2 = diff(y(t), t) == c*a1*x*y-d*y-a2*y*z/(y+a2);
Eqn3 = diff(z(t), t) == sigma*z^2-beta*z^2/(y+b2);
The first command is
% Define the parameter values
p = 1;
k = 2;
a1 = 0.5;
y = 1; % define the value of y
% Define the initial condition
x0 = 0.1;
% Define the time span for the simulation
tspan = [0 10];
% Define the differential equation as a function handle
f = @(t, x) p*x*(1 - x/k) - a1*x*y;
% Solve the differential equation using ode45
[t, x] = ode45(f, tspan, x0);
% Plot the solution
plot(t, x);
xlabel('Time');
ylabel('x'); and the second is
% Define the parameter values
c = 0.1;
a1 = 0.01;
d = 0.02;
a2 = 0.005;
b2 = 1;
% Define the initial conditions
x0 = 1;
y0 = 0.5;
z0 = 1;
% Define the time span for the simulation
tspan = [0 100];
% Define the differential equation as a function handle
f = @(t, y) c*a1*x0*y - d*y - (a2*y*z0)/(y+b2);
% Solve the differential equation using ode45
[t, y] = ode45(f, tspan, y0);
% Plot the solution
plot(t, y);
xlabel('Time');
ylabel('y'); and finally the third is
% Define the parameter values
sigma = 0.8;
beta = 0.9;
b2 = 0.5;
% Define the initial conditions
x0 = 1;
y0 = 1;
z0 = 1;
% Define the time span for the simulation
tspan = [0 10];
% Define the differential equations as a function handle
f = @(t, xyz) (sigma*xyz(3)^2 - beta*xyz(3)^2/(xyz(2) + b2));
% Solve the differential equation using ode45
[t, z] = ode45(f, tspan, [x0, y0, z0]);
Error using odearguments
@(T,XYZ)(SIGMA*XYZ(3)^2-BETA*XYZ(3)^2/(XYZ(2)+B2)) returns a vector of length 1, but the length of initial conditions vector is 3. The vector returned by
@(T,XYZ)(SIGMA*XYZ(3)^2-BETA*XYZ(3)^2/(XYZ(2)+B2)) and the initial conditions vector must have the same number of elements.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
% Plot the solution
plot(t, z(:,3));
xlabel('Time');
ylabel('z');
Rik on 10 Mar 2023
You are speaking in riddles. If you want help, you will have to provide information I can work with. If you ignore my question and post a comment I don't understand, I can't help you.

Fangjun Jiang on 10 Mar 2023
Edited: Fangjun Jiang on 10 Mar 2023
The three equations form a non-linear ODE with three independent variables (x,y,z). Because they are coupled, you can't solve them by solving one equation at a time. Your approach is incorrect.
Based on your code, when you solve Eqn1, you are replacing variable y with a constant value y=1. And there is an error in your definition of "f", comparing to Eqn1.
Well, at least for Eqn1 and Eqn2, there were no errors executing your code. But the results are incorrect and they are basically meaningless.
For Eqn3, the error you had is related to mis-matched size. The "f" seems to define an one-state ODE. The variable "xyz" seems to match "z" in Eqn3. But the initial condition was giving as a 1x3 vector [x0 y0 z0]. The "f" seems to want to make "xyz" a three-state variable since xyz(3) and xyz(2) are used.
Again, incoherient errors in this definiton of "f" for Eqn3, even when it is compared to the logic used in "f" definition for Eqn1 and Eqn2.
The correct approach to solve this problem can be found in this link, under "Solve Nonstiff Equation", where an example of 2-state nonlinear ODE is provided.

### Categories

Find more on Programming in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!