Func returns a vector of length 2, but the length of initial conditions vector is 9.

1 次查看(过去 30 天)
%% We will create a function that refers to our system and its variables
function dx=mbk(t,x)
m=10; % mass
b=0; % value of spring which creates oscillation
k=1600;
F0=0;
sigma=1;
Fext=F0*sin(sigma*t); % harmonic function
dx(1)=x(2);
dx(2)=1/m*(-b*x(2)-k*x(1)+Fext); % mechanical system equation
dx=dx';
this is my function,
clear
clc
% This code is going to solve our system function
tspan=[0 10]; % time interval
n= -4:4; %initial condition
delta_n=[0,0,0,0,1,0,0,0,0]
[T, X]=ode23('mbk',tspan,delta_n); % ode23 command solves differantial equations
displacement=X(:,1);
v=X(:,2);
%% Results
figure(1)
plot(T,displacement)
xlabel('Time/sec')
ylabel('Displacement')
grid on
figure(2)
plot(T,v)
xlabel('Time/sec')
ylabel('Velocity')
grid on
and this one is my solution for function, im trying to give the system a unit impulse thats why i created delta_n but now it says
"mbk returns a vector of length 2, but the length of initial conditions vector is 9."
how can i solve this problem ^^

回答(1 个)

William Rose
William Rose 2021-11-27
Your initial conditions should be a 2-element vector, since mbk() returns a 2-componnt vector.
So do
tspan=[0 10]; % time interval
%n= -4:4; %initial condition
%delta_n=[0,0,0,0,1,0,0,0,0]
x0=[-4,4]; %or x0=[0,1]; or whatever you wish the initial conditions to be
[T, X]=ode23('mbk',tspan,x0); % ode23 command solves differantial equations to use
Try.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by