MATLAB Answers

Unary operator '-' is not supported for operand of type 'function_handle' in for loop function

91 views (last 30 days)
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; %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
%--------------------------------------------------------------------------------------------------------%
A = @(t) 1/2*(a2-a1)*tanh(10.*((t-tmid)/((t2-t1))))+(1/2*(a2+a1));
t = 0:120; %used for plotting x axis
D = @(t) (p*c.*A(t))/2*m; %drag constant
plot(t, A(t), '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]);
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
t(n) = tn; %build time vector, needed for plotting
Dn = D(n).*(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
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; %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(t,ay(t), 'b-', t, ax(t), 'r-'); %axis between 0, where t is less than tg. Roughly set out for now, needs to be worked after loop is fixed.

  0 Comments

Sign in to comment.

Accepted Answer

Michael Croucher
Michael Croucher on 17 Sep 2020
You have defined D as a function but in line 36 you are not using it as a function. You have defined it as a function of a variable t so you need to give it some value of t. I'm not sure about what you are calculating but it needs to be something like
-(D(tn)) * vy*v(n)+g;
This causes a different error because D(tn) is a scalar while vy is a vector. Do you mean something like
-(D(tn)) * vy(n)*v(n)+g;
Similar issue with D on line 45.

  3 Comments

Liam Crocker
Liam Crocker on 17 Sep 2020
Ahh my apologies I missed the vy(n).
I think you may be right, because It gives me the formula for Ax pre written into the code as seen above. I think the V line is missing an n as well. I'll try these solutions out and if not come back back/ seek clarity on the question. Thank you!!!
Liam Crocker
Liam Crocker on 17 Sep 2020
Hi Matthew, sorry for not including this in my original question.
I've been asked to delete the last values of x y vx and vy. How would I do this?

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!