Different number of elements error.
Show older comments
I'm trying to model the motion of a pendulum and in my code when I run it to display the different values, I get an error that it is "Unable to perform assignment because the left and right sides have a different number of elements." I haven't seen this error before and I don't understand whats wrong. Does anyone have any ideas?
theta0 = pi/3; g = 9.81; L = 1;
t = 0; tf = 20; dt = 0.005;
nt = (tf-t0)/dt;
t = linspace(t0,tf,nt);
theta = zeros(1,nt);
theta(1) = theta0;
omega = zeros(1,nt);
alpha = zeros(1,nt);
Etotal = zeros(1,nt);
H = L - L*cos(theta);
f = 'Time'; g = 'Omega'; h = 'Theta'; p = 'Alpha'; q = 'Energy';
fprintf('Time \t \t Omega\t \t Theta\t \t Alpha \t \t Energy\n',f, g, h, p, q)
fprintf('%5.2f \t %5.2f \t %5.2f \t %5.2f\n',t,omega,theta,alpha,Etotal);
while t <= 20
t = t + .5;
for k = 1:1:nt-1
omega(k+1) = -g/L*sin(theta(k))*dt + omega(k);
theta(k+1) = omega(k)*dt + theta(k);
alpha(k+1) = (omega(k+1) - omega(k))/dt;
Etotal = g*H + (1/2)*(L*omega)^2;
end
fprintf('%5.2f \t %5.2f \t %5.2f \t %5.2f\n',t,omega,theta,alpha,Etotal);
end
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!