MATLAB Homework Problem: "Incorrect use of '=' operator" in a for loop
Show older comments
Hi, everyone - I'm hoping to touch up some old homework, but I'm running into a smidge of a problem. The title is more or less the crux of the issue - the program throws an error at me whenever I try to use a for loop.
Below should be all of the relevant code, I'd appreciate any and all assistance with this.
HW5_MEE380.mlx
% plot minimum launch speed (fplot)
% seperate plot launch angle as a function of coordinates (fplot)
nd = (15-2)/0.5;
hd = (11+2)/0.5;
[X,Y] = meshgrid(2:0.5:15,-2:0.5:12)
Z1 = [];
Z2 = [];
for (i = 2:0.5:15 && j = -2:0.5:12)
[v0,theta] = calcMinSpeedShot(i,j);
Z1 = [Z1 v0];
Z2 = [Z2 theta];
end
calcMinSpeedShot.mlx
function [v0,theta] = calcMinSpeedShot(xT,yT)
g = 9.81; % acceleration of gravity (m/s^2)
syms x_s y_s theta_s;
if(xT < 0)
error('calcMinSpeedShot: xT must be positive.')
end
% x must be a positive value
if(atand(yT/xT) >= 85)
error('calcMinSpeedShot: angle to hit target is too steep.')
end
% >85deg is too steep
f_v0 = sqrt((g*x_s^2) ./ (2*(cos(theta_s)).^2.*(x_s*tan(theta_s) - y_s))); % original v0 eqn
f_dv0 = diff(f_v0); % derivative of v0 wrt to theta
f_dv0_num = subs(f_dv0,[x_s,y_s],[xT,yT]); % substituting in given values
f_dv0_num_rootfind = @(theta) double(subs(f_dv0_num,theta_s,theta)); % substituting symbolic theta for testable theta
theta = findRootBisect(f_dv0_num_rootfind,atan(yT/xT),(atan(yT/xT)+1),0.0001); % findRootBisect to find appropriate theta
f_v0_ans = subs(f_v0,[x_s,y_s,theta_s],[xT,yT,theta]);
v0 = f_v0_ans(theta);
% same steps from 1 - 4 now in a function
end
findRootBisect.mlx
function [r,counter] = findRootBisect(f,x1,x2,err)
x_test = 0.0; %x_test will be our floating midpoint of each interval
counter = 0.0;
found = false;
if(f(x1)*f(x2) > 0.0)
error('findRootBisect: Root not bracketed.')
end
if(err <= 0.0)
error('findRootBisect: Error must be positive.')
end
while (found == false)
x_test = (x1 + x2) / 2;
if(f(x_test) == 0 || (x2 - x1) < err)
r = x_test;
found = true;
counter = counter + 1;
else
if(f(x_test)*f(x1) < 0)
x2 = x_test;
counter = counter + 1;
else
x1 = x_test;
counter = counter + 1;
end
end
end
1 Comment
Daniel O'Dette
on 2 Apr 2020
Answers (2)
James Tursa
on 2 Apr 2020
This:
for (i = 2:0.5:15 && j = -2:0.5:12)
Needs to be two nested loops:
for i = 2:0.5:15
for j = -2:0.5:12
1 Comment
Daniel O'Dette
on 2 Apr 2020
Walter Roberson
on 2 Apr 2020
for (i = 2:0.5:15 && j = -2:0.5:12)
You need to use a nested loop instead, such as
for i = 2:0.5:15
for j = -2:0.5:12
code goes here
end
end
5 Comments
Daniel O'Dette
on 2 Apr 2020
Walter Roberson
on 3 Apr 2020
f_v0 = sqrt((g*x_s^2) ./ (2*(cos(theta_s)).^2.*(x_s*tan(theta_s) - y_s))); % original v0 eqn
theta = findRootBisect(f_dv0_num_rootfind,atan(yT/xT),(atan(yT/xT)+1),0.0001); % findRootBisect to find appropriate theta
so you are passing in atan(yT/xT) as the second parameter, to become theta_s . Now look inside f_v0 to the denominator:
(x_s*tan(theta_s) - y_s)
substitute theta_s = atan(yT/xT) and by definition of atan(), atan(tan(yT/xT)) is going to be yT/xT . Substitute that into the expression:
(x_s*yT/xT - y_s)
now let x_s = xT and y_s = yT and xT*yT/xT - yT = yT - yT = 0. So that term would come out as 0. It is being multipled by something, so no matter what that other thing is (provided it is not infinite) the result would be 0 for the denominator, which gives you a divide by 0.
Daniel O'Dette
on 3 Apr 2020
Walter Roberson
on 3 Apr 2020
Start from atan(yT/xT) plus something small instead of from atan(yT/xT) exactly.
Daniel O'Dette
on 3 Apr 2020
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!