How to solve and plot a differential equation

31 ビュー (過去 30 日間)
AUSTIN WHITELEY
AUSTIN WHITELEY 2022 年 1 月 18 日
回答済み: Vandit 2023 年 6 月 28 日
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.
  2 件のコメント
James Tursa
James Tursa 2022 年 1 月 18 日
You have numeric values for M and F?
AUSTIN WHITELEY
AUSTIN WHITELEY 2022 年 1 月 18 日
yes.
M= [1.5 -.12; -1 .4];
F=[1; 0];

サインインしてコメントする。

回答 (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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by