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

조회 수: 68 (최근 30일)
Dan Teep
Dan Teep 2013년 11월 7일
댓글: Brian Wilkinson 2021년 12월 17일
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
Walter Roberson
Walter Roberson 2018년 11월 15일
(1/2)*(1+erf(p-mi./(sigma*sqrt(2))))
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)
Deep Patel
Deep Patel 2018년 3월 29일
Thank you very much

댓글을 달려면 로그인하십시오.

카테고리

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