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

629 views (last 30 days)

Show older comments

Dan Teep
on 7 Nov 2013

Commented: Brian Wilkinson
on 17 Dec 2021

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
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)

### Accepted Answer

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

Kalyan Dash
on 6 Feb 2018

### More Answers (2)

Walter Roberson
on 7 Nov 2013

Edited: Walter Roberson
on 7 Nov 2013

fzero((x) F(x)-90, [0, 100])

##### 2 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!