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'.

 Réponse acceptée

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.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Coder dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by