Simple question: How to find the 'x' at a certain value of y(x) equation?

This may be a simple question. But let's assume I have one ugly equation:
x = [0:10];
y = @(x) x.^2.*12./23./23.9.*log(x).^2
How do I find the value of 'x' where y = 30?
Thanks!

4 Comments

I am new to matlab and in general coding.
I was writing a code for taylors series in matlab so I thought what if i input the function from user itself and the solve for that. But to do that i need to find the function value at x=0, How should i proceed with this.(just curious)
Thanks a lot..
How to find the value of x in this equation: sin(x)=0.2
Did you tried the approach that is mentioned in the accepted answer?
@Ceylin, Which intersection do you want to solve for x?
syms f(x)
f(x) = sin(x);
fplot(f, [-2*pi, 2*pi]), grid on % draw left side of Eqn
yline(0.2, 'r-') % draw right side of Eqn
xlabel('x')
legend('sin(x)', '0.2')

Sign in to comment.

 Accepted Answer

This works:
x_for_y30 = fzero(@(x)y(x)-30, 50)
x_for_y30 =
14.0341

8 Comments

Thanks! What does the 50 mean in that formula?
My pleasure!
The 50 is a starting guess for fzero. It needs to have an initial estimate of the solution (as do all iterative gradient-descent algorithms, such as nonlinear parameter estimation or function minimisation algorithms such as fminsearch). The fzero function will then start there to search for a zero-crossing. Since I subtracted 30 from your function to create a zero-crossing there, it then uses the initial estimate to find the value of ‘x’ at the zero-crossing, in this instance, the value of your function at y=30 (that it thinks is y=0).
So does that number need to be 'less' than the anticipated result?
Thanks
No. The number, 30 here, can be anything the function is defined at for any value of ‘x’. So for instance:
y = @(x) sin(x);
would return NaN because sin(x) would never equal 30.
If you’re interested in the initial estimate for fzero, that can be anything, provided that (1) the function is defined at the initial estimate, (2) is defined for all values between it and the value of ‘x’ at the zero-crossing, and (3) that a zero-crossing exists. So there are some restrictions, but only those imposed by the continuity (and differentiability) of the function, and the behaviour of the function on the interval of interest. For instance, if you have a function with more than one zero, in order to find all of them, you have to loop through appropriate initial estimates for the zero-crossings. That can get complicated, so I won’t go into it here.
Thanks! That was a great explanation!
As always, my pleasure!
That’s what we’re here for!
If I have x and want y then how could I compute that?
Actually there is an additional way of solving this, using interp1, especially if the actual function is not available —
x = [0:15]+eps;
y = @(x) x.^2.*12./23./23.9.*log(x).^2
y = function_handle with value:
@(x)x.^2.*12./23./23.9.*log(x).^2
yq = 30;
x_for_y30 = interp1(y(x), x, yq) % Get 'x' For Specific 'y'
x_for_y30 = 14.0322
y_for_x2pi = interp1(x, y(x), 2*pi) % Get 'y' For Specific 'x'
y_for_x2pi = 2.9555
figure
plot(x, y(x))
grid
hold on
plot(x_for_y30, 30, 'ms', 'MarkerFaceColor','m')
plot(2*pi, y_for_x2pi, 'cs', 'MarkerFaceColor','c')
hold off
xline(2*pi,'--k', '2\pi')
yline(y_for_x2pi, '--k', sprintf('y=%.2f for x=2\\pi',y_for_x2pi))
xline(x_for_y30, '--k', sprintf('x=%.2f for y=30',x_for_y30))
.

Sign in to comment.

More Answers (1)

Alpha
If you plot the following
x=[-100:.1:100]
f = @(x) x.^2.*12./23./23.9.*log(x).^2
y=f(x)
plot(x,y)
grid on
place the marker on the point that shows y=30 f(x) is not symmetric, it has 2 zeros, and f=30 on 2 places:
x01=14.04
x02=-29.5
if what you really mean is:
f2 = @(x) x.^2.*12./(23.*23.9).*log(abs(x)).^2
then the function is symmetric and there are 2 values of x that satisfy your question:
x01=14.04
x02=-14.04
Compare both functions and y=30
If you find this answer of any help solving this question, please click on the thumbs-up vote link
thanks in advance
John

Asked:

A
A
on 28 Feb 2016

Commented:

on 17 Apr 2024

Community Treasure Hunt

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

Start Hunting!