initial condition vector longer than ODE vector?

조회 수: 1 (최근 30일)
Will
Will 2023년 4월 18일
댓글: Will 2023년 4월 18일
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