How do I find x for a given value of y?

254 views (last 30 days)
I'm stuck on this, and no one seems to understand the question. I've defined several variables and made x an array of values. I then made an anonymous function and used fplot to graph it's outputs(F in this case) for every x value. Now, I just need to find the x value that give me F=90. The only way I can think to do it is to solve for x by hand and then type that into matlab but there has to be a simpler way. Any body know how to do this? Here's up to where I'm stuck:
% Clear all windows and variables
clc
% Input the values for mass, height, friction coefficient, and gravity
m=18; % Mass (kilograms)
h=10; % Height (meters)
mu=0.55; % Friction coefficient (no units)
g=9.81; % Gravitational acceleration (m/s^2)
% Input a reasonable range for x
x=[-100:100];
% Calculate F given the range of x
F=@(x) (mu.*m.*g.*(h.^2 + x.^2).^(1/2))/(x+mu.*h);
% Plot F as a function of x
fplot(F,[0,100])
title('Force versus Distance')
xlabel('Distance (meters)')
ylabel('Force (newtons)')
grid on
% Find the x value that gives F=90
??????????????
  3 Comments
Brian Wilkinson
Brian Wilkinson on 17 Dec 2021
Here is a code I created that solves for x given y.
% This code solves for x given a value of y. Just set the value for x_low,
% x_high, and y. Enter the formula in terms of x inside the for loop and
% set it equal to y_i.
x_low=-100; %the lowest value of x that the for loop could possibly output
x_high=100; %the highest value of x that the for loop could possibly output
y=90; %known value of y
x=(x_low+x_high)/2;
for i=1:1000
m=18; % Mass (kilograms)
h=10; % Height (meters)
mu=0.55; % Friction coefficient (no units)
g=9.81; % Gravitational acceleration (m/s^2)
y_i=(mu.*m.*g.*(h.^2 + x.^2).^(1/2))/(x+mu.*h);
if y_i<y %the greater than or less than sign may need to be flipped to get a correct value of x after the for loop completes
x=x-((x_high-x_low)/(2^(i+1)));
else
x=x+((x_high-x_low)/(2^(i+1)));
end
end
fprintf('x= %f',x)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Nov 2013
Add this code to the bottom of your code:
Fx = (mu.*m.*g.*(h.^2 + x.^2).^(1/2)) ./ (x+mu.*h);
% Plot the function.
figure;
plot(x, Fx, 'b-');
grid on
% Find the x value that gives F=90.
% This happens when the difference between Fx and 90 is a minimum.
[difference, index_At_F_Equals_90] = min(abs(Fx-90))
% Get the x value at that index. Print to command window.
x90 = x(index_At_F_Equals_90)
  7 Comments
Walter Roberson
Walter Roberson on 6 Feb 2018
Edited: Walter Roberson on 6 Feb 2018
abs(Fx-target) will be closer to 0 at the place where the values in Fx are closer to the target value. The location in Fx that is closest to the target will have the smallest absolute difference compared to the target. min() applied to that vector determines the location where the minimum value is. min() with a single output would be just the minimum value itsef; min() with two outputs like is given here also returns the index in the vector at which the minimum was found. So after the min() line, the variable difference will reflect how close you were able to get to the target, and index_At_F_Equals_target will be the location (index) in Fx that was closest. Then you access the vector of x values at that index to determine which x value it was that gave rise to the Fx that was closest to the target. xtarget will not be function after this: it will be the numeric x value at which Fx became closest to the target.
Kalyan Dash
Kalyan Dash on 6 Feb 2018
Thanks Walter. It is much clear now after reading the description. I have used it in my code and it worked.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 7 Nov 2013
Edited: Walter Roberson on 7 Nov 2013
fzero((x) F(x)-90, [0, 100])
  2 Comments
Dan Teep
Dan Teep on 7 Nov 2013
Hmmm, I'm getting an error message at the column with the function name. I guess I'll try using a function file real quick instead of an anonymous one.
Walter Roberson
Walter Roberson on 7 Nov 2013
Darn, somehow I deleted a character
fzero(@(x) F(x)-90, [0, 100])

Sign in to comment.


Deep Patel
Deep Patel on 29 Mar 2018
how to solve 4*exp(-x^2)*sin(x) = 1 for x in matlab?
  4 Comments
Walter Roberson
Walter Roberson on 29 Mar 2018
For the symbolic toolbox you would use
syms x
solve(4*exp(-x^2)*sin(x) == 1)
If you are trying to work in a MuPad notebook (which are probably going to be completely gone by R2018b), then
numeric::solve(4*exp(-x^2)*sin(x) = 1)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!