Using user input to solve for a variable in a function
14 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!