Using user input to solve for a variable in a function

21 ビュー (過去 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 Center および File ExchangeEquation Solving についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by