MATLAB: Common error But i dont understand it yet. Unable to perform assignment because the left and right sides have a different number of elements.

errorprojectile

I'm trying to get the projectile with drag to show up on the graph. I think I understand how to plot it, but I can't get the drag loop to work.
% This program computes three projectiles motion, velocity, and accleration of a sphere with and without
%air resistance
clear;
close all;
clc
%% Sphere
rad = 0.037; % sphere radius
mass = 0.147; % mass spherekg
Cd = 0.47; % Drag coefficient
Af = pi*rad^2; % cross section area sphere
%% air density Gravitiy
rho = 1.2; % air density
g = 9.81; % gravitational acceleration
%% Initial condition: position and velocity
yo = 2; % initial position
V1 = 15; % initial velocity
xo = 0; %intial position
d=45 ; %degreess
theta = d*pi/180;
%% Time vector and time step
dt = 0.1; % time step
t = 0:dt:10; % time vector
imax = length(t);
%% Velocity and position no drag
acc1 = -g; % acceleration with no drag
x = V1*t*cos(theta);
y = V1*t*sin(theta)- 0.5*g*t.^2;
% velocity no drag
% position no drag
vx = V1 * cos(theta);
vy = V1 * sin(theta)-0.5*g*t.^2;
V1 = sqrt(vx^2 +vy.^2);
%% terminal velocity with drag
Vter = sqrt(mass*g/(0.5*Cd*rho*Af));
%% Acceleration velocity and position with drag
%% Pre set
accx2 = zeros(size(t)); % initialize acceleration with drag
accy2 = zeros(size(t));
Fdx = zeros(size(t)); % initialize force with drag
Fdy = zeros(size(t));
Vx2 = zeros(size(t)); % initialize velocity with drag
Vy2 = zeros(size(t));
V2 = zeros(size(t));
y2 = zeros(size(t)); % initialize position with drag
x2 = zeros(size(t));
% Initialize previous state
yprv = yo; % initialize previous position (initial condition)
xprv = xo;
Vxprv = vx; % initialize previous velocity (initial condition)
Vyprv = vy;
%% loop drag force, acceleration, velocity and position
for i=1:imax
Fdx(i) = 0.5.*Cd.*rho.*Af.*Vxprv.^2; % Drag force
Fdy(i) = 0.5.*Cd.*rho.*Af.*Vyprv.^2;
accx2(i) = Fdx(i)/mass - g; % Acceleration
accy2(i) = Fdy(i)/mass - g;
Vx2(i) = Vxprv + accx2(i)*dt; % Velocity
Vy2(i) = Vyprv + accy2(i)*dt;
x2(i) = xprv + Vx2(i)*dt;
y2(i) = yprv + Vy2(i)*dt; % Position
yprv = y2(i); % Update previous position
xprv = x2(i);
Vxprv = V2(i); % Update previous velocity
Vyprv = V2(i);
end
%% drag no drag
figure(1)
plot(x,y,'b:'), hold on
plot(x2,abs(y2),'g'), hold off
%% Subplot for acceleration, velocity, position and drag force
figure(2)
subplot(2,2,1)
plot([t(1), t(end)],[acc1, acc1],'b'); hold on
plot(t,acc2,'r')
xlabel('time, s')
ylabel('acceleration, m/s^2')
legend('No drag', 'Drag')
title('Acceleration vs. time')
subplot(2,2,2)
plot(t,abs(V1),'b'); hold on;
plot(t,abs(V2),'r'); hold on
plot([t(1), t(end)],[Vter, Vter],'--k')
xlabel('time, s')
ylabel('Velocity, m/s')
legend('No drag', 'Drag','Terminal Vel.')
title('Velocity vs. time')
subplot(2,2,3)
plot(t,vy,'b'); hold on
plot(t,y2,'r');
xlabel('time, s')
ylabel('y position, m')
legend('No drag', 'Drag')
title('Position vs. time')
subplot(2,2,4)
plot(t,Fd,'r')
xlabel('time, s')
ylabel('Drag force, N')
title('Drag force vs. time')

Best Answer

Vyprv is a 101 element vector, not a scalar so you can't stick that into a single element of an array. Maybe you meant Vyprv(i) instead, sut I didn't delve into the code, like you should do. Figure out why Vyprv is a vector but Vxprv is not.