How do i write this differential equation in matlab?

global R C u
R = 10e3;
C = 3.3e-6;
tau = R*C;
u = @(t) 10*(t>tau);
The initial value is uC(0)=0
I want to write the diff equation in a function and use ode45 to solve it. Can someone please help me? i've tried everything but i dont understand how to write it into a function and how to solve it. Would be really appreciated!

 Réponse acceptée

There are several options, including using the numerical solvers.
This approach uses the Symbolic Math Toolbox —
syms C R uc(t) u
eqn = R*C*diff(uc)+uc == u;
uc(t) = dsolve(eqn, uc(0)==0)
uc(t) = 
uc = subs(uc,{C,R,u},{1E-9,1E+3,sin(2*pi*t)})
uc(t) = 
figure
fplot(uc, [0 10])
grid
.

5 commentaires

Thank you for the fast response!
Is there a more simple way to solve this without using any toolboxes?. I want to solve this using pure programming from scratch
I also want to solve it using ode45
That is relatively straightforward —
syms C R uc(t) u Y
eqn = R*C*diff(uc)+uc;
[VF,Sbs] = odeToVectorField(eqn)
VF = 
Sbs = 
fcn = matlabFunction(VF,'Vars',{t,Y,C,R})
fcn = function_handle with value:
@(t,Y,C,R)[-Y(1)./(C.*R)]
Then, write your function to add ‘u(t)’ to it using the value of ‘t’ provided by ode45 (or whatever solver you decide to use). Either use a direct function call, or interp1 if ‘u(t)’ is a vector that neds to be interpolated to a particular time in each step.
EDIT — (12 May 2021 at 21:08)
to add ‘u(t)’ is straightforward —
fcn = @(t,Y,C,R) [-Y(1)./(C.*R)] + u(t);
Provide whatever ‘u(t)’ is, and it will work.
I got it working, big thanks for the help!
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by