Using fzero to find the second intercept of the two curves.

1 view (last 30 days)
So I have function f(x)=-ln(x+0.01)+e^x that plots the cross sectional area of a shape and then a horizontal line segment given by g(x)=f(x)x=0. I need to find the second intercept of the curves. Easy to visual if you graph it. Now I need to use the fzero function to solve this and I have been searching for hours for an example like this and can't find anything similar.

Answers (2)

John D'Errico
John D'Errico on 14 Aug 2017
Do you need to provide a start point for fzero? (Yes.)
Does fzero work best if you provide a bracket (two points), that surrounds the root in question? (Yes.)
So if you know roughly where the root of interest is, then what is the problem? You cannot use fzero if you do not provide a start point at least.

John BG
John BG on 17 Aug 2017
Hi Andy
With the following you get both zero points of g(x)=f(x)*x=0
1.
1st point with fsolve start point x01=0
close all;clear all;clc;
f1=@(x) log(x+.01)+exp(x)-x*(log(x+.01)+exp(x))
x01=0;
options = optimoptions('fsolve','Display','iter');
options.MaxFunctionEvaluations = 1000
y1=fsolve(f1,x01,options)
f1 =
function_handle with value:
@(x)log(x+.01)+exp(x)-x*(log(x+.01)+exp(x))
options =
fsolve options:
Options used by current Algorithm ('trust-region-dogleg'):
(Other available algorithms: 'levenberg-marquardt', 'trust-region')
Set properties:
Display: 'iter'
MaxFunctionEvaluations: 1000
Default properties:
Algorithm: 'trust-region-dogleg'
CheckGradients: 0
FiniteDifferenceStepSize: 'sqrt(eps)'
FiniteDifferenceType: 'forward'
FunctionTolerance: 1.000000000000000e-06
MaxIterations: 400
OptimalityTolerance: 1.000000000000000e-06
OutputFcn: []
PlotFcn: []
SpecifyObjectiveGradient: 0
StepTolerance: 1.000000000000000e-06
TypicalX: 'ones(numberOfVariables,1)'
UseParallel: 0
Show options not used by current Algorithm ('trust-region-dogleg')
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 12.9973 377 1
1 4 4.02556 0.0344646 49.7 1
2 6 0.712178 0.0809282 7.6 1
3 8 0.050945 0.0937519 1.1 1
4 10 0.000680152 0.046356 0.0991 1
5 12 2.09695e-07 0.00686238 0.00168 1
6 14 2.17453e-14 0.000124849 5.41e-07 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
y1 =
0.262487926852786
.
2.
2nd zero point with fzero starting x02=100
x02=100;
y2=fsolve(f1,x02,options)
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 7.08218e+90 7.15e+90 1
1 4 9.58373e+89 0.989999 9.68e+89 1
2 6 1.29689e+89 0.989899 1.31e+89 1
3 8 1.75496e+88 0.989797 1.77e+88
..
104 210 0.591762 0.36162 3.44 1.34
105 212 0.0156953 0.171887 0.383 1.34
106 214 4.06374e-05 0.0410109 0.0175 1.34
107 216 4.00574e-10 0.00232187 5.46e-05 1.34
108 218 4.0027e-20 7.33586e-06 5.46e-10 1.34
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
y2 =
1.000000000073332
.
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

Categories

Find more on Optimization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!