Using user input to solve for a variable in a function
18 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Jason Millen
on 16 Aug 2016
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 Comments
See Also
Categories
Find more on Equation Solving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!