Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 2-by-1. Error in linear2d (line 7) z(:,1)=[0;1];

1 view (last 30 days)
close all;
dt = 0.1;%time step
u1=1;
u2=1;
c = u2-u1;
u = sign(u2-u1);
z(:,1)=[0,1];
k=0.12;
m=2.21;%initial parameters
v=1;
a=1.4;
tend=1000;
t=0;
i=1
while t < tend-2*dt
vh=v(i)-dt*k*z(1,i)/ (2*m);
z(2,i) = z(1,i)+ dt*vh;
%a(i+1)= -k*z(i)/2;
v(i+1)= vh-dt*k*z(2,i)/ (2*m);% + dt*a(i+1)/2;
i = i+1;
t = t + dt
end
plot(z);

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Jun 2019
Edited: KALYAN ACHARJYA on 25 Jun 2019
You defined v=1, as scalar, but you called the function as vector v(i)
vh=v(i)-dt*k*z(1,i)/ (2*m);
%....^.........
When i=1, then v(1)=??
When i=2, then v(2)=?? in iterations.
Also the while loops runs multiple times
As t < tend-2*dt, where t=0 and tend=1000, dt=0.1, On such case the size of z also not sufficient
vh=v(i)-dt*k*z(1,i)/ (2*m);
%...............^.........
Because z(:,1)=[0,1] allows only two iterations.
Requested to read about array indexing.
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Jun 2019
Yes, I have hinted where the error is, I have no idea what you doing inside the code. You have to properly define the v and z or you have to change the code. Please try to undestand why you getting the error? Probably you may solve it afterwards.

Sign in to comment.


Advait Sinha
Advait Sinha on 25 Jun 2019
I am new to matlab so I’m finding it confusing and hard

Tags

Community Treasure Hunt

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

Start Hunting!