MATLAB Answers

Use of ODE45 System equation

17 views (last 30 days)
fabian
fabian on 30 Aug 2015
Answered: Steven Lord on 30 Aug 2015
Hello Friends, I am using the ode45 commands to solve the equation of the system, but I have problems with the input arguments of the function, because it is a system of two equations but also have an input vector is variable, I ask them for their cooperation . Thank you.
clear
clc
f=10; %frequency of sine wave in Hz
overSampRate=30; %oversampling rate
fs=overSampRate*f; %sampling frequency
duty_cycle=70; % Square wave with 50% Duty cycle (default)
% nCyl = 5; %to generate five cycles of sine wave
T=0:1/fs:1; %time base
u=square(2*pi*f*T,duty_cycle); %generating the square wave
u=(u+1)/2;
[t,y]=ode45('onda_cuadrada1',transpose(T),[0 0]);
%%%%%%%%%%%%%%%%%%%%%%%%%%5
function dy = onda_cuadrada1(t,y)
R=1000;
Rl=100;
L=41.02e-3;
C=22.51e-9;
% F=10; %frequency of sine wave in Hz
% overSampRate=30; %oversampling rate
% Fs=overSampRate*F; %sampling frequency
% duty_cycle=70; % Square wave with 50% Duty cycle (default)
% l=0:1/Fs:1; %time base
% u=square(2*pi*F*l,duty_cycle); %generating the square wave
% u=(u+1)/2;
dy = zeros(2,1); % a column vector
dy(1) =-(Rl/L)*y(1)-(1/L)*y(2)+(1/L)*u;
dy(2) =(1/C)*y(1)-(1/(R*C))*y(2);

Answers (1)

Steven Lord
Steven Lord on 30 Aug 2015
Don't specify your ODE function as a string. Define onda_cuadrada1 to accept three input arguments (t, y, and u) then pass an anonymous function as the first input to ODE45. See the section of the ODE45 documentation on parameterizing functions for examples how to pass your u vector into your ODE function as an additional argument.

Community Treasure Hunt

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

Start Hunting!