Help to not get stuck in a loop while returning multiple arrays from a local function?

1 view (last 30 days)
I have to find the minimum and maximum angle of a catapult's trajectory that will make it over a wall. I have a local function that includes the motion equations and returns the distance at which the object lands, an array of x values, an array of y values, and the number of x and y values based on an input of lots of conditions called anglesfunction. In the main function, I tried to call it multiple times in a loop but it must be an infinite loop because I have to quit the program. It always quits on the same line, which is: [temp(p), a, b, c(p)] = anglefunction(v0, w, m, d, h, dw, theta2(p), k); I have it commented out right now, but I tried to do a similar thing later to do bisection and it stops at the same point there too. Here's my full code, g is gravity and is the only thing I am passing in to the main function.
if true
% code
function [mid] = longassignment3(g)
v0 = 40;
w = 5;
m = 10;
d = 50;
h = 5;
dw = 30;
theta1 = 38;
k = .05;
n = 1;
%hold on
%grid on
A = 0:.01:h;
%plot(dw, A, 'r', 'linewidth',2)
%plot(d, 0, 'linewidth', 2)
[distance, x, y, z] = anglefunction(v0, w, m, d, h, dw, theta1, k); %should take out eventually
%plot(x,y);
min = 0;
max = 0;
q = 1;
tol = .3;
tester = 2;
theta2 = 0:.01:90;
p = 1;
dt = 1e-3;
num = 1;
[temp(p), a, b, c(p)] = anglefunction(v0, w, m, d, h, dw, theta2(p), k); %for p=1
while theta2(p) < 90
p = p + 1; %p becomes 2
[temp(p), a, b, c(p)] = anglefunction(v0, w, m, d, h, dw, theta2(p), k); %for p=2 the first time
B = size(a);
while q <= B(2);
if a(q) >= dw & a(q) <= (dw+tol)
num = q;
end
q = q + 1;
end
if b(num) >= (h - tol) || b(num) <= (h+tol) %if it hits wall
min = 0;
max = 0;
else %doesn't hit the wall
if temp(p)<temp(p-1)
min = theta2(p); %not assigning max and min
elseif temp(p)>temp(p-1)
max = theta2(p);
end
end
end
%min
%max
%distance = 0;
%q = 1;
%min = 26;
%max = 45;
%u = 1;
%done = 1;
%while done ~= 0
% total = min + max;
% mid = total/2;
% [distance(u), a, b, c(u)] = anglefunction(v0, w, m, d, h, dw, mid, k);
% if distance(u) < d
% max = mid;
% elseif distance(u) > d
% min = mid;
% elseif distance(u) >= d-tol | distance(u) <= d+tol
% done = 0;
% end
% u = u + 1;
%end
end
if true
% code
function [dist, x, y, n] = anglefunction(v0, w, m, d, h, dw, theta1, k)
n = 1;
g = 9.81;
%hold on
%grid on
A = 0:.01:h;
clear vx vy x y
%plot(dw, A, 'r', 'linewidth',2)
%plot(d, 0, 'linewidth', 2)
x(n) = 0;
y(n) = 0;
dvx = 0;
dvy = 0;
dt = 1e-3;
vx(n) = (v0)*(cos(theta1*pi/180));
vy(n) = (v0)*(sin(theta1*pi/180));
while y(n)>=0 && x(n)>=0
veff(n) = sqrt((vx(n) - w)^2 + (vy(n))^2);
dvx(n) = -k*veff(n)*(vx(n) - w)*dt/m;
dvy(n) = (-k*veff(n)*vy(n)/m-g)*dt;
vx(n+1) = vx(n) + (dvx(n));
vy(n+1) = vy(n) + (dvy(n));
x(n+1) = x(n) + (vx(n))*dt;
y(n+1) = y(n) + (vy(n))*dt;
n = n +1 ;
end
dist = x(n);
end
end end

Answers (0)

Categories

Find more on Line Plots 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!