MATLAB Answers

For Loop Error, unable to perform operation, LHS incompatible with RHS

4 ビュー (過去 30 日間)
Liam Crocker
Liam Crocker 2020 年 9 月 16 日
回答済み: Liam Crocker 2020 年 9 月 17 日
So I have this for while loop that I am calculating, and what I think is the problem is that it's trying to stuff 121 values into one definition. However when I do things by hand it should equal to 0 whilst n is 1 at the moment. So my ax where the line errors shoiuld become 0. As tn = (n-1)*T with T being .005. If N is 1, then 1-1) * .005 should still be 0, so the Dn line will be 0 whilst N is 1, the next line V will be 50 as square root of 2500 is 50. So my ax(n) is equal to -0 * vx (which has v0 defined as 50) * v(defined as 50 in the previous line). Ax(1) = -0*50*50, which should become 0 whilst N is one. So I'm half puzzled as to why it's giving me the error (stated in title), Im guessing it's trying to put one of these 1x121 doubles into it, but im not sure why...
As always I really appreciate the help I get. It is late when posting this, so my apologies if I do not accept your answer straight away. Thank you to the Matlab forums for the continued help in my coding, learning lots with your support which is greatly appreciated!
clc
clear
%--------------------------------------------------------------------------------------------------------%
p = 1.2; %variables given to me in question
g = 9.81;
m = 77;
c = 1;
a1 = .7;
a2 = 50;
t1 = 50;
t2 = 70;
h0 = 50;
T = .005;
v0 = 50;
tmid = (t1+t2)/2; %formula gives to find what t mid is
tA = 0:120; %used for plotting x axis
%--------------------------------------------------------------------------------------------------------%
if tA<=tmid
Area = a1;
else Area = a2;
Area = 1/2*(a2-a1)*tanh(10.*((tA-tmid)/((t2-t1))))+(1/2*(a2+a1));
end
D = p*c.*Area /2*m;
plot(tA, Area, 'b-', 50, .7, 'ro', 70, 50, 'ro') %t1 occurs at 50 seconds in, the area is .7 so I used that as a y co-ordinate and 50 as my x, marking that spot with a red circle.
axis([0 120, .6 70]); % line above continued : Same sort of thing with area 2, except this occurs after 60 seconds
xlabel('Time')
ylabel('Area')
title('Area over Time')
%--------------------------------------------------------------------------------------------------------%
x(1) = 0;, vx(1) = v0;, y(1) = 0;, vy(1) = 0; %initial variables used to start the loop
for n=1 %initialize time index
while 1 %forever while loop
if y(n) > h0, break; end % break when ground is reached
tn = (n-1)*T; %nth time instant in seconds
tA(n) = tn; %build time vector, needed for plotting
Dn = D.*(tn); %drag constant in time tn
v = sqrt(vx(n)^2+vy(n)^2); %problem tells me to write "no need to save V in an array"- not sure what this means
ax(n) = -Dn * vx(n) *v; %horizontal acceleration
ay(n)= -D*vy*v(n)+g;
x(n+1) = x(n)+T*vx(n);
vx(n+1)= vx(n)+T*ax(n);
y(n+1)=y(n)+T*vy(n);
vy(n+1)= vy(n)+T*ay(n);
n = n+1; %updates the index to use N+1, so second round will use 2 for N, as N is defined as 1, N= 1+1, whill use 2 for N in next loop and so on.
end
end
%--------------------------------------------------------------------------------------------------------%
Vc = sqrt(g/D) %equation to be used later
%--------------------------------------------------------------------------------------------------------%
plot(tA,ay(tA), 'b-', tA, ax(tA), 'r-'); %axis between 0, where t is less than tg. Roughly set out for now, needs to be worked after loop is fixed.

  0 件のコメント

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

採用された回答

Liam Crocker
Liam Crocker 2020 年 9 月 17 日
My problem is that the for loop is set up for function handles not vectors. I would need to change the code to make it work for vectors.

  0 件のコメント

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by