Using user input to solve for a variable in a function

25 visualizaciones (últimos 30 días)
katkim
katkim el 15 de Ag. de 2016
Respondida: Jason Millen el 16 de Ag. de 2016
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?

Respuestas (1)

Jason Millen
Jason Millen el 16 de Ag. de 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
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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by