function argument pow error

조회 수: 7 (최근 30일)
Cesar
Cesar 2025년 9월 2일
댓글: Umar 2025년 9월 3일
not sure why I'm getting this error, any help will be appreciated. thanks
function topic4_p2_expand_then_bisect()
f = @(x) exp(-x) - x;
x0 = 0; d = 0.1; dmaxpow = 6;
% expand symmetrically until sign change or limit
found = false; pow = -1;
E = []; % expansion log [pow, a, b, f(a), f(b)]
end
while pow < dmaxpow
pow = pow + 1;
a = x0 - d; b = x0 + d;
fa = f(a); fb = f(b);
E(end+1,:) = [pow, a, b, fa, fb];
if fa*fb <= 0, found = true; break; end
d = 2*d;
end
Unrecognized function or variable 'pow'.
write_csv('p2_expansion.csv', E);
if ~found, error('Failed to bracket a root.'); end
% bisection
tol = 1e-8; maxit = 100;
k=0; hist=zeros(maxit,5);
while k<maxit
k=k+1; m=0.5*(a+b); fm=f(m);
hist(k,:)=[k,a,b,m,fm];
if abs(fm)<tol || 0.5*(b-a)<tol, break; end
if f(a)*fm<0, b=m; else, a=m; end
end
hist=hist(1:k,:);
write_csv('p2_bisect.csv', hist);
% plot f on bracket
xs = linspace(a,b,400); ys = zeros(size(xs));
for i=1:length(xs), ys(i)=f(xs(i)); end
figure; plot(xs,ys,'-'); hold on; yline(0,'k--');
xlabel('x'); ylabel('f(x)'); title('f on final bracket'); grid on; hold off;
function write_csv(fname, M)
fid=fopen(fname,'w');
for i=1:size(M,1)
for j=1:size(M,2)
fprintf(fid,'% .16e',M(i,j));
if j<size(M,2), fprintf(fid,'');
end; fprintf(fid,'\n');
end; fclose(fid);
end
end

채택된 답변

Umar
Umar 2025년 9월 2일
편집: Umar 2025년 9월 2일

Hi @Cesar,

Let me address your comment:” not sure why I'm getting this error, any help will be appreciated. thanks"

The error you're encountering is due to a syntax issue with a misplaced end statement in your code that's creating a variable scope problem.

The Root Cause: In your code, you have a stray end statement that prematurely terminates your function before the while loop executes:

found = false; pow = -1;
E = []; % expansion log [pow, a, b, f(a), f(b)]
end  % ← This end statement terminates the function here
while pow < dmaxpow  % MATLAB throws error: pow is now out of scope

The Fix: Simply remove the misplaced end statement on line 6:

function topic4_p2_expand_then_bisect()
f = @(x) exp(-x) - x;
x0 = 0; d = 0.1; dmaxpow = 6;
% expand symmetrically until sign change or limit
found = false; pow = -1;
E = []; % expansion log [pow, a, b, f(a), f(b)]
% Remove the misplaced 'end' here
while pow < dmaxpow
  pow = pow + 1;
  a = x0 - d; b = x0 + d;
  fa = f(a); fb = f(b);
  E(end+1,:) = [pow, a, b, fa, fb];
  if fa*fb <= 0, found = true; break; end
  d = 2*d;
end
% ... rest of your code remains unchanged

Why This Happens: When MATLAB encounters that premature end, it thinks your function is complete. The subsequent while loop appears to be outside any function scope, so the variable pow (defined inside the now-terminated function) becomes unrecognized.

This should resolve your error completely. Your expand-then-bisect algorithm implementation is well-structured otherwise.

  댓글 수: 2
Cesar
Cesar 2025년 9월 2일
Right thank you. Is this well-written? thanks
function h = height_1(t)
% Rocket height function
% Input: t (scalar or vector time values)
% Output: h (same size as t, rocket height)
% Initial parameters
g = 9.8; % acceleration due to gravity (m/s^2)
v0 = 125; % initial velocity (m/s)
h0 = 500; % initial height (m)
% Compute raw trajectory
h = -0.5 * g * t.^2 + v0 * t + h0;
% Special cases
h(t < 0) = h0; % negative times → fixed at initial height
h(h < 0) = 0; % ground impact → capped at 0
end
% test script or Command Window
time_values = 0:1:10; % vector of times from 0 to 10 seconds
heights = height_1(time_values);
disp(heights);
format long g
height_1(10)
Umar
Umar 2025년 9월 3일

Hi @Cesar,

I ran your updated height_1(t) function and it works great — no errors this time.

A few things I noticed:

  • The formula you used matches the standard physics equation for height under gravity.
  • Using elementwise operators (.^, .*) makes the code handle both single numbers and vectors of time values smoothly.
  • I like how you handled special cases:For negative time, the function just returns the starting height & after the rocket hits the ground, the height stays at 0 instead of going negative.

When I tested it with time_values = 0:1:10, the results looked correct. At t = 10, the function returned 1260, which matches the expected calculation.

Overall, the code is clear, easy to read, and gives the right results. If you want, you could test a bit further out in time to show the rocket hitting the ground, but as it is, the function looks solid.

Nice work cleaning this up!

Attached Screenshot

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by