How to solve and plot a differential equation

23 次查看(过去 30 天)
Use Matlab to solve for the matrix 𝑴 and the vector 𝐹⃗ . Plot the concentration in each compartment vs. time. dC/dt + 𝑀𝐶⃗ = 𝐹⃗
C= [c1; c2]
when dC/dt=0, c1=.8333 c2=2.08333.
Here's what I have in terms of code
C=[c1; c2];
M= [1.5 -.12; -1 .4];
F=[1; 0];
syms c(t)
ode= diff (c,t) + M*C == F
sol=dsolve(ode)
fplot(sol,[0,5]);
It is having trouble with the fact that C is a vector with two other variables.

回答(1 个)

Vandit
Vandit 2023-6-28
Hi,
Below is the updated MATLAB code that solves the given differential equation and plot the concentration in each compartment over time :
M = [1.5 -0.12; -1 0.4];
F = [1; 0];
c1=.8333
c2=2.08333;
C0 = [c1; c2];
tspan = [0 10]; % Adjust the time span as needed
% Solve the differential equation
[t, C] = ode45(@(t, C) M*C + F, tspan, C0);
% Plot the concentration in each compartment vs. time
figure;
plot(t, C(:, 1), 'b', 'LineWidth', 2); % Compartment 1
hold on;
plot(t, C(:, 2), 'r', 'LineWidth', 2); % Compartment 2
xlabel('Time');
ylabel('Concentration');
legend('Compartment 1', 'Compartment 2');
title('Concentration vs. Time');
grid on;
I have also attached the output plot which should be obtained after running the above code in MATLAB.
To know more about the Ordinary Differential Equations refer to the link below:
Hope this helps.
Thankyou

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by