Hi Matlab forums, hope you are all having a good day.If anyone could spare some time to help me,I have encounted the error called "Unary operator '-' is not supported for operand of type 'function_handle' in for loop function". Does it mean I cant have a negative added onto the functional handle which calculates the drag? I have been giving a image of the specifc formula's and such I am meant to use and told not to change them, so it leads me to a problem in my code. I have tried putting brackets around the drag, so it is -1*(drag) but this does not work with or without brackets. So I'm kind of stuck where the problem is other than line 36 in which matlab tells me. I've attatched my script as a file (renamed just for clarity) and pasted it here. I've also attatched an image of all the formula's and code provided to me which should (hopefully!) assist in diagnosing my error.
I apologise if this type of question has been answered before, I did do a search on the matlab answers page and could only find one problem that was slightly similar to mine.
Any and all help is greatly appreciated forum support, thank you in advance for helping me with my problem!!
clc
clear
p = 1.2;
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;
A = @(t) 1/2*(a2-a1)*tanh(10.*((t-tmid)/((t2-t1))))+(1/2*(a2+a1));
t = 0:120;
D = @(t) (p*c.*A(t))/2*m;
plot(t, A(t), 'b-', 50, .7, 'ro', 70, 50, 'ro')
axis([0 120, .6 70]);
xlabel('Time')
ylabel('Area')
title('Area over Time')
x(1) = 0;, vx(1) = v0;, y(1) = 0;, vy(1) = 0;
for n=1
while 1
if y(n) > h0, break; end
tn = (n-1)*T;
t(n) = tn;
Dn = D(n).*(tn);
v = sqrt(vx(n)^2+vy(n)^2);
ax(n) = -Dn * vx(n)*v;
v(n) = sqrt(vx(n)^2+vy(n)^2);
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;
end
end
Vc = sqrt(g/D)
plot(t,ay(t), 'b-', t, ax(t), 'r-');
0 Comments
Sign in to comment.