MATLAB Homework Problem: "Incorrect use of '=' operator" in a for loop

2 views (last 30 days)
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

Answers (2)

James Tursa
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
Daniel O'Dette on 2 Apr 2020
I appreciate the quick answer! Unfortunately now I'm getting a battery of new errors...
Error using symengine
Division by zero.
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
f_dv0_num_rootfind = @(theta) double(subs(f_dv0_num,theta_s,theta)); % substituting symbolic theta for testable theta
if(f(x1)*f(x2) > 0.0)
theta = findRootBisect(f_dv0_num_rootfind,atan(yT/xT),(atan(yT/xT)+1),0.0001); % findRootBisect to find appropriate theta

Sign in to comment.


Walter Roberson
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
Walter Roberson
Walter Roberson on 3 Apr 2020
Start from atan(yT/xT) plus something small instead of from atan(yT/xT) exactly.
Daniel O'Dette
Daniel O'Dette on 3 Apr 2020
The good news is that that no longer derives a "divide by zero" error.
The bad news is that the program now ends spitting out my own error and stopping on rotation one.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!