initial condition vector longer than ODE vector?

1 次查看(过去 30 天)
In my code i have to solve an RCL equation 3 times with the ODE45 command for different values of omega. Upon running the code i get the following error:
OMEGA_1 returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by OMEGA_1 and the initial conditions vector must have the same
number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in ProjQ3B (line 21)
[T,Y]=ode45(@omega_1,tspan,[0;0]);
I am not sure how to proceed with properly defining either the vector or initial conditions to clear the error. Can anyone advise? my full code is below:
clear;clc;close all
a=2;
b=5;
% v0 is now set to 10
v0=10;
% Using a,b to determine rest of variables
R=10*a;
C=0.0001/a;
L=0.01*b;
% omega is set to (0.5, 1, 2)*sqrt(L*C)
omega=0.5*sqrt(L*C);
omega2=1*sqrt(L*C);
omega3=2*sqrt(L*C);
tspan=[0 (40*L)/R];
[T,Y]=ode45(@omega_1,tspan,[0;0]);
function I1=omega_1(t,i)
I1=@(t,i)[i(2);-(R/L)*i(2)-(i(1)/(L*C))+((v0*omega)/L)*(cos(omega*t))];
end
function I2=omega_2(t,i)
I2=@(t,i)[i(2);-(R/L)*i(2)-(i(1)/(L*C))+((v0*omega)/L)*cos(omega2*t)];
end
function I3=omega_3(t,i)
I3=@(t,i)[i(2);-(R/L)*i(2)-(i(1)/(L*C))+((v0*omega)/L)*cos(omega3*t)];
end

采纳的回答

Alan Stevens
Alan Stevens 2023-4-18
Do it like this:
a=2;
b=5;
% v0 is now set to 10
v0=10;
% Using a,b to determine rest of variables
R=10*a;
C=0.0001/a;
L=0.01*b;
% omega is set to (0.5, 1, 2)*sqrt(L*C)
omega=0.5*sqrt(L*C);
omega2=1*sqrt(L*C);
omega3=2*sqrt(L*C);
tspan=[0 (40*L)/R];
[T,Y]=ode45(@(t,i) omega_1(t,i,omega,R,C,L,v0),tspan,[0;0]);
plot(T,Y)
function I1=omega_1(t,i,omega,R,C,L,v0)
I1=[i(2);-(R/L)*i(2)-(i(1)/(L*C))+((v0*omega)/L)*(cos(omega*t))];
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by