필터 지우기
필터 지우기

Using user input to solve for a variable in a function

조회 수: 12 (최근 30일)
katkim
katkim 2016년 8월 15일
답변: Jason Millen 2016년 8월 16일
So I have an unknown x (my variable) and a, k, t1 and t2 are values that are obtained from the user. I used the input function to obtain a value for a, k, t1, and t2.
example: p1 = 'What is value for a? '; a = input(p1);
Then I want to take those input values and put them into this equation: f = @(x)(x-a)/(k*(t1-t2))
then finally solve for x based on the inputs.
Is it possible to do something like this?

답변 (1개)

Jason Millen
Jason Millen 2016년 8월 16일
I think what you might be looking for is the solve() function. It allows you to pass in an equation and a variable that you would like to solve for and will solve the equation for the variable. For example, you can do:
syms x
eqn = sin(x) == 1;
solx = solve(eqn,x)
solx =
pi/2
You can find the full documentation for solve() here.
If you are just asking if what you are doing is possible, it believe it is. The following worked for me:
a = input('enter value for a')
enter value for a 0
a =
0
b = input('enter value for k')
enter value for k 3
k =
3
t1 = input('enter value for t1')
enter value for t1 2
t1 =
2
t2 = input('enter value for t2')
enter value for t2 1
t2 =
1
f = @(x)(x - a)/(k*(t1 - t2))
f =
function_handle with value:
@(x)(x-a)/(k*(t1-t2))
f(9)
ans =
3

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by