Matlab Function fzero and Error using fzero (line 273)
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello Guys!
I'm Kinda new to matlab, so be gentle with me!
Im doing my homework for my university, and while i was writing the code and using fzero function, it gaves me this error ( not a the first zeroes i want to know but at the second ) :
Error using fzero (line 273) The function values at the interval endpoints must differ in sign.
im calculating two different zeroes ( one and two ) of a referement function of my own.
ill post here the code to be more clear :
-----
Function :
funref = @(x) 2.7*(1./(1+5*(x-1).^2))+4.52*(1-(2*exp(x))./(1+5*x.^2));
---
Graph :
x = linspace(-1,2);
plot(x,funref(x),[-1,2],[0,0], ':r')
hold on
text (-0.53,0, 'first zero');
text (0.55, 0, 'Second Zero');
text (0.2,-4.4, 'Minimum' );
text (1.2, 3.25, 'Maximum');
axis ([-1 2 -5 5]);
y = linspace(-5,5);
line([0.091 0.091],[y(1),y(end)], 'lineWidth', 1);
text (0.1, 4, 'Minimum abscissa');
line([1.08 1.08], [y(1),y(end)], 'lineWidth', 1);
text (1.1, -2, 'Maximum abscissa' );
--- Zeroes Approximation :
firstzero = fzero(funref,[-0.53,0])
-- AND HERE I GOT THE DAMN PROBLEM : --
secondzero = fzero(funref,[0.55,0])
Here it gave me the error :
Error using fzero (line 273)
The function values at the interval endpoints must differ in sign.
im driving crazy, i really dont know where im mistaking.
Please help me! Thank you a lot.
1 commentaire
Walter Roberson
le 14 Avr 2016
By the way, notice you reversed the 0 and 0.55 in your second call. That would not have caused the problem you are observing though.
Réponses (1)
Steven Lord
le 14 Avr 2016
Add two circles to your plot, indicating the endpoints of the interval over which your second fzero call is searching for a zero.
funref = @(x) 2.7*(1./(1+5*(x-1).^2))+4.52*(1-(2*exp(x))./(1+5*x.^2));
x = linspace(-1,2);
plot(x,funref(x),[-1,2],[0,0], ':r')
hold on;
plot([0, 0.55], funref([0, 0.55]), 'ko')
Now do you see the problem? If the value of your function at the two endpoints has the same sign, you may not have a solution on that interval and so fzero says it's not going to search for something it's not sure exists. [Granted, there are intervals where the two endpoints have the same sign but there are multiple zeros inside the interval, like if you'd asked for a zero on the interval [-1, 1]. This is a limitation of fzero with the interval syntax.]
If you stretch your interval slightly, to something like [0 0.6], you'll find that second zero.
0 commentaires
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!