When does an anonymous function reach a specified value?

8 views (last 30 days)
I get the feeling this a rather simple question, but I cannot seem to get this to work how I want.
Consider an equation, say, y = e^(a*x). I want to find the first x-value where y crosses a threshold, say, y>100. The function is written in MATLAB as an anonymous function, i.e.,
y = @(x) exp(a*x);
How would I go about solving for x? Ideally I want something more specific/robust then simply plotting the function and eyeballing/using the plot tools to estimate the x-value.

Accepted Answer

Torsten
Torsten on 5 Sep 2023
Edited: Torsten on 5 Sep 2023
threshold = 100;
a = 0.5;
fun = @(x) exp(a*x) - threshold;
solx = fsolve(fun,1)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
solx = 9.2103
exp(a*solx)
ans = 100.0000

More Answers (2)

John D'Errico
John D'Errico on 5 Sep 2023
Since you do not provide a value for a, there is no numerical solution possible.
However, as long as the function is a simple one like this, the answer is just as simple. I'll use the symbolic toolbox. We just need to solve for the value of x.
syms x a
ytarget = 100;
yfun = exp(a*x);
xtarget = solve(yfun == ytarget,x)
xtarget = 
And that gives us a nice EXACT analytical result. For any value of a, we can know the value of that would achieve the desired target value for y. We can even turn this into a function we can use.
xfun = matlabFunction(xtarget)
xfun = function_handle with value:
@(a)(log(1.0e+1).*2.0)./a
For example, when a==0.5, the answer will be...
format long g
xfun(0.5)
ans =
9.21034037197618
Easy enough. Of course, this will not always be the case. Then you might need to resort to numerical methods.

Dyuman Joshi
Dyuman Joshi on 5 Sep 2023
It's not possible to determine the exact value which satisfies the condition you have stated, but you can get an approximate value, by finding the minimum value of the function, which will depend upon the tolerance you define -
Using fminsearch with tolerance of 10^-10.
%Random value
a=2;
val=100;
%Original function
y1 = @(x) exp(a*x);
%Function to be minimised
y2 = @(x) abs(y1(x)-val);
%Starting point
x0 = 0;
%Set options as required
options = optimset('Display','iter','TolX',1e-10);
x = fminsearch(y2,x0,options)
Iteration Func-count f(x) Procedure 0 1 99 1 2 98.9995 initial simplex 2 4 98.9985 expand 3 6 98.9965 expand 4 8 98.9925 expand 5 10 98.9844 expand 6 12 98.968 expand 7 14 98.9344 expand 8 16 98.864 expand 9 18 98.7089 expand 10 20 98.3322 expand 11 22 97.2171 expand 12 24 92.2515 expand 13 26 39.9306 expand 14 28 39.9306 contract inside 15 30 0.233253 contract outside 16 32 0.233253 contract inside 17 34 0.233253 contract inside 18 36 0.233253 contract inside 19 38 0.233253 contract inside 20 40 0.233253 contract inside 21 42 0.233253 contract inside 22 44 0.166879 contract inside 23 46 0.0329868 contract inside 24 48 0.0329868 contract inside 25 50 0.0170172 contract inside 26 52 0.00798172 contract inside 27 54 0.0045185 contract inside 28 56 0.00173142 contract inside 29 58 0.00139359 contract inside 30 60 0.000168901 contract inside 31 62 0.000168901 contract inside 32 64 0.000168901 contract inside 33 66 2.64113e-05 contract inside 34 68 2.64113e-05 contract inside 35 70 2.24168e-05 contract inside 36 72 1.99725e-06 contract inside 37 74 1.99725e-06 contract inside 38 76 1.99725e-06 contract inside 39 78 1.05451e-06 contract inside 40 80 4.71367e-07 contract inside 41 82 2.91572e-07 contract inside 42 84 8.98975e-08 contract inside 43 86 8.98975e-08 contract inside 44 88 5.46999e-09 contract inside 45 90 5.46999e-09 contract inside 46 92 5.46999e-09 contract inside 47 94 5.46999e-09 contract inside Optimization terminated: the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-10 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-04
x = 2.3026
format long
%x value
x
x =
2.302585093021396
%Value of the original function at the point
y1(x)
ans =
1.000000000054700e+02
%Value of the function that was minimised at the point
y2(x)
ans =
5.469985353556694e-09
'TolX' is the termination tolerance for x. Read the documentation page of fminsearch for more info.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!