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

272 次查看(过去 30 天)
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 个评论
Brian Wilkinson
Brian Wilkinson 2021-12-17
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)

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2013-11-7
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 个评论
Walter Roberson
Walter Roberson 2018-2-6
编辑:Walter Roberson 2018-2-6
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 2018-2-6
Thanks Walter. It is much clear now after reading the description. I have used it in my code and it worked.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2013-11-7
编辑:Walter Roberson 2013-11-7
fzero((x) F(x)-90, [0, 100])
  2 个评论
Dan Teep
Dan Teep 2013-11-7
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 2013-11-7
Darn, somehow I deleted a character
fzero(@(x) F(x)-90, [0, 100])

请先登录,再进行评论。


Deep Patel
Deep Patel 2018-3-29
how to solve 4*exp(-x^2)*sin(x) = 1 for x in matlab?
  4 个评论
Walter Roberson
Walter Roberson 2018-3-29
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MuPAD 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by