Can anyone tell me why I am getting "Unrecognized function or variable 'a'." in this code?

g=@(x) atan(4*x);
d=@(x) 4/(16*x^2 + 1);
h=1;
for i= 0:h:10
if (g(i)*g(i+h)<0)
a=(i);
b=(i+h);
break;
end
end
x0 = (a+b)/2;
Unrecognized function or variable 'a'.

 채택된 답변

g=@(x) atan(4*x);
d=@(x) 4/(16*x^2 + 1);
h=1;
found_it = false;
for i= 0:h:10
if (g(i)*g(i+h)<0)
a=(i);
b=(i+h);
found_it = true;
break;
end
end
if found_it
x0 = (a+b)/2;
else
fprintf('sign change not found!\n');
end
sign change not found!
fplot(g, [0 10])
g(0)
ans = 0
g is 0 at 0, but it never changes sign in the [0 10] range. You are not testing for <= 0, you are testing for < 0.
... and be careful, if a g(i) equals 0 exactly you might determine x0 incorrectly.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by