How to solve and plot a differential equation

26 views (last 30 days)
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.

Answers (1)

Vandit
Vandit on 28 Jun 2023
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

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!