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

조회 수: 2 (최근 30일)
Daniel O'Dette
Daniel O'Dette 2020년 4월 2일
댓글: Daniel O'Dette 2020년 4월 3일
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
Daniel O'Dette
Daniel O'Dette 2020년 4월 2일
Quick clarification, the error is with the for loop in HW5_MEE380.mlx

댓글을 달려면 로그인하십시오.

답변 (2개)

James Tursa
James Tursa 2020년 4월 2일
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
Daniel O'Dette
Daniel O'Dette 2020년 4월 2일
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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 4월 2일
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
Walter Roberson
Walter Roberson 2020년 4월 3일
Start from atan(yT/xT) plus something small instead of from atan(yT/xT) exactly.
Daniel O'Dette
Daniel O'Dette 2020년 4월 3일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by