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

7 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

VBBV
VBBV il 25 Set 2021
Modificato: VBBV il 25 Set 2021
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 Commenti

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 25 Set 2021
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.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by