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

7 views (last 30 days)
Arda Canbas
Arda Canbas on 27 Nov 2021
Answered: William Rose on 27 Nov 2021
%% 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 ^^

Answers (1)

William Rose
William Rose on 27 Nov 2021
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.

Community Treasure Hunt

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

Start Hunting!