Question about how to solve a differential equation
3 views (last 30 days)
Show older comments
Here are my main script and function script. C stands for concentraion and t is time, the question asked me to solve the time range when C2 drops from 150 to 75.
%Set the initial conditions
C_init=[150,150];
%C=C_init
%define time range of analysis: measure in hours
t_min=0;
t_max=10;
t_range=[t_min,t_max];
%call ODE45 solver
[tim,Con]=ode45(@compartmental,t_range,C_init)
%generate a plot of each concentration
plot(tim,Con(:,1),'-',tim,Con(:,2),'.')
xlabel('Time(hours)');
ylabel('Concentrations of urea in the fluids of the two compartments');
legend('Compartment 1','Compartment 2');
%
Here are my function scripts:
function Ct=compartmental(t,C)
%System of differential equations to be solved; 1st order
%
%First written by YZ on 2/17/2012
%Last updated by YZ on 2/17/2012
%
%Incoming arguments:
%C=concentration of urea;independent variables
%t=time;dependent variable
%Solutions
%Ct=dC(2)/dt
%
%Calls:parameters
%
[R, K, V]=parameters;%this sets the values for the parameters
%in the equations
Ct=zeros(2,1); %initialize a column vector and fill with zeros
%
Ct(1)=(R-K(1)*C(1)+K(2)*C(2))/V(1);
Ct(2)=(K(1)*C(1)-K(2)*C(2)-K(2)*C(2))/V(2);
%
end %function compartmental
function [R,K,V]=parameters71
%Value for parameters
%
%First written by YZ on 2/17/2012
%Last updated by YZ on 2/17/2012
%
%Values for R
%Values for K
%Values for V
R=100;
K=[33,33];
V=[10,25];
%
end %function parameters71
However, I got several errors:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in compartmental (line 19)
Ct(1)=(R-K(1)*C(1)+K(2)*C(2))/V(1);
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in YZHW71 (line 18)
[tim,Con]=ode45(@compartmental,t_range,C_init)
I know the first error ask me to erase C(2) and K(2) in the equation, but do I have to change the original equation. And I have no idea what the rest of errors said. Really appreciate if you help me solve this.
0 Comments
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!