differential equations matlab with 2 equations with changing values dependent on each other

24 vues (au cours des 30 derniers jours)
So for my project, we have to replicate plots, the first one relating to two differential equations where the values of the derivatives are dependent on the changing values of the other. Essentially, the derivative of one variable is dependent on the derivative of the other and vice versa. Im confused since the function cannot be solved simply and plotted simply. I was advised to do this via vectors and do matrix math, but the details arent clear. Heres my code, let me know! The functions are
dxu= r*xu-r*xu.^2/k-du*xu*xe/(he+xe)
dxe=pe*xu*xm/(hv+xu)-de*xe-dt*xu*xe
with all values except for xu xe and t being the constant variables. we have to solve these for values and plot them over a time interval. I know how to do the plotting, the solving is where im struggling with the most. Thanks!
clc; clear;
r=0.927; k=1.8182*10^8; du=2; he=10^3;
tspan=1:150;pe=0.4;hv=10000;xm=1;de=0.1;dt=.0000000005;
syms xu xe
dxu=[ r*xu -r*xu.^2/k -du*xu*xe/(he+xe)];
dxe=[pe*xu*xm/(hv+xu) -de*xe -dt*xu*xe];
dxe2=diff(dxe);
ans=dxu+dxe+dxe2
ans2=solve(ans==dxe2)
  3 commentaires
Jon
Jon le 2 Déc 2022
Oh sorry, I didn't realize you were doing this with the symbolic toolbox. I actually don't have any experience with that product. I have always solved them in MATLAB using the functions in the hyperlink above. Perhaps someone else can assist you if are using the symbolic toolbox
John D'Errico
John D'Errico le 2 Déc 2022
Modifié(e) : John D'Errico le 2 Déc 2022
I made your code more readable. Learn to use the buttons on top of the edit window. They are there to help you.
I would note it is a really bad idea to use ans as an explicitly declared variable, as it will constantly get stepped on. That is just a bad idea in terms of programming style in MATLAB.
Next, you wrote this:
dxu=[ r*xu -r*xu.^2/k -du*xu*xe/(he+xe)];
dxe=[pe*xu*xm/(hv+xu) -de*xe -dt*xu*xe];
making dxu and dxe VECTORS of length 3. Those spaces in there, and the [] brackets ell MATLAB they are vectors. Yet the equations you wrote are not for vectors. They are just 3 terms in a sum. So even though you wrote that I think you meant to write this:
dxu = r*xu-r*xu.^2/k-du*xu*xe/(he+xe);
dxe = pe*xu*xm/(hv+xu)-de*xe -dt*xu*xe;
Finally, you seem to think you need to use solve. Instead, these are DIFFERNTIAL equations. Solve them using dsolve.
Honestly, there are surely other problem in what you need to do. But this will get you started.

Connectez-vous pour commenter.

Réponses (1)

Sam Chak
Sam Chak le 3 Déc 2022
Not exactly sure what to do dxe2 = diff(dxe). But from the given ODEs, they can be solved to obtain and .
You can later perform numerical differentiation or whatever manipulations that involve the state variables and .
tspan = [0 150];
x0 = [1 1.5];
[t, x] = ode45(@odefcn, tspan, x0);
xu = x(:,1);
xe = x(:,2);
subplot(2,1,1)
plot(t, xu), grid on, ylabel('x_u')
subplot(2,1,2)
plot(t, xe), grid on, ylabel('x_e'), xlabel('t')
function xdot = odefcn(t, x)
xdot = zeros(2, 1);
r = 0.927;
k = 1.8182e8;
du = 2;
he = 1e3;
pe = 0.4;
hv = 10000;
xm = 1;
de = 0.1;
dt = 0.0000000005;
xu = x(1);
xe = x(2);
xdot(1) = r*xu - r*xu^2/k - du*xu*xe/(he + xe);
xdot(2) = pe*xu*xm/(hv + xu) - de*xe - dt*xu*xe;
end

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by