Problem with fzero function
8 views (last 30 days)
Show older comments
I had some troubles in using fzero.
Here is my script: lambda=0.23; T_orb=86400; D=130*1000; omega=2*pi/T_orb; distance=1000*[37700;38400;39000;39600]; for q=1:4 APS.tint(q)=fzero(@(T_int)[distance(q)*lambda/(2*D/2*-(cos(omega*T_int+omega*(T_orb/4-T_int/2))-cos(omega*(T_orb/4-T_int/2))))-2000],100);
end
and it gives me as result tint=[1.527152190882263e-12 1.527032893943011e-12 1.526723991273891e-12 1.527144367230523e-12], but using this values the function has not a zero! Using a bigger first guess (like 1000) it finds the correct first solution (the function is periodical). Why it gives me this result of magnitude 10^-12?
1 Comment
jgg
on 22 Feb 2016
If your function is non-linear, fzero can find local near-zeroes. You will want to trial it from several points.
I can't debug your code further because you didn't include lambda.
Accepted Answer
Star Strider
on 22 Feb 2016
‘Why it gives me this result of magnitude 10^-12?’
That means that the value of ‘T_int’ it estimates is close to zero.
You left out ‘lambda’, so I created a value for it. I have no idea what its true value should be, so if I chose the wrong value, your function could have true zeros, but it does not for the ‘lambda’ I chose.
However, your function does not have any true zero-crossings in the sense that fzero can locate them. Instead, it has discontinuities with sign-reversals, so you need to search for them. Define the ‘T_int’ vector here to have the range and resolution (number of points) you need to define the sign-reversal points with acceptable accuracy.
The code:
lambda = 1000; % <— Insert Correct Value Here
T_orb=86400;
D=130*1000;
omega=2*pi/T_orb;
distance=1000*[37700;38400;39000;39600];
fcn = @(T_int)[distance(q).*lambda./(2*D/2*-(cos(omega*T_int+omega*(T_orb/4-T_int/2))-cos(omega*(T_orb/4-T_int/2))))-2000];
T_int = linspace(0, 5E5, 100);
y = fcn(T_int);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
zero_cross_idx = zci(y); % Approximate Zero-Crossing Indices Of Your Function
figure(1)
plot(T_int, y)
hold on
plot(T_int(zero_cross_idx), y(zero_cross_idx), '*r')
hold off
grid
2 Comments
Star Strider
on 23 Feb 2016
Even with ‘lambda’ defined, my Answer remains the same. Plot it and you will see the reason.
Your function is periodic, however it periodically goes to +Inf then goes to -Inf and does not cross y=0 between them, so your function has no true zero crossings in the sense that fzero can determine them.
Testing for the +Inf to -Inf sign changes is the only way to detect the periodicity. The plot function connects the +Inf and -Inf values with a line, but that line is not an actual zero-crossing. Your function is not defined between the +Inf and -Inf transitions, so it does not cross zero there.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!