Unable to perform assignment because the left and right sides have a different number of elements.

1 回表示 (過去 30 日間)
Here is my code and the error I am getting. I tried everything but coudnt solve it.
ini_dis= input('Enter the value of initial displacement : ');
ini_vel= input('Enter the value of initial velocity : ');
ti= input('Enter the value of initial time : ');
tf= input('Enter the value of final simulation time : ');
m= input('Enter the value of Mass : ');
d= input('Enter the value of Damping constant : ');
k= input('Enter the value of spring constant : ');
tspan=[ti tf];
x0=[ini_dis ini_vel];
[T X]=ode45('fy',tspan,x0);
Here is the function written seperately in function file:
f
unction dx = fy(t,x)
%This function determines the drivatives of the states
% solves spring mass damper system
global m d k;
F0=0;
omega=1;
F=F0*sin(omega*t);
dx(1)=x(2); %converting first order to 2nd
dx(2)=(F-d*x(2)-k*x(1))/m;
dx=dx';
The error I am getting:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in fy (line 11)
dx(2)=(F-d*x(2)-k*x(1))/m;
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in WEEK2_A5_spring_mass_system (line 17)
[T X]=ode45('fy',tspan,x0);

採用された回答

VBBV
VBBV 2021 年 9 月 25 日
編集済み: VBBV 2021 年 9 月 25 日
clearvars
% ini_dis= input('Enter the value of initial displacement : ');
% ini_vel= input('Enter the value of initial velocity : ');
% ti= input('Enter the value of initial time : ');
% tf= input('Enter the value of final simulation time : ');
% m= input('Enter the value of Mass : ');
% d= input('Enter the value of Damping constant : ');
% k= input('Enter the value of spring constant : ');
ini_dis = 3;
ini_vel = 10;
ti = 0;
tf = 100;
m = 1.2;
d = 0.05;
k = 1.01;
tspan=[ti tf];
x0=[ini_dis ini_vel];
[t x]=ode45(@(t,x) fy(t,x,m,d,k),tspan,x0);
plot(t,x)
function dx = fy(t,x,m,d,k)
%This function determines the drivatives of the states
% solves spring mass damper system
%global m d k;
F0=0;
omega=1;
F=F0*sin(omega*t);
dx(1)=x(2); %converting first order to 2nd
dx(2)=(F-d*x(2)-k*x(1))/m;
dx=dx';
end
  3 件のコメント
Akshay Chandel
Akshay Chandel 2021 年 9 月 25 日
Thanx for the edited script. Script is clear and on point,

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 9 月 25 日
global m d k;
One of those variables is not initialized as a global variable.
When you declare a variable to be global, it does not mean that every function and script that uses that name is sharing the same variable. Only functions and scripts that declare the variable global share the variable.
You assign values to m, d, and k in your script, but you do not declare them to be global in the script, so you are assigning to a local variable not to the global variable.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by