Working with an equation and variables from user.

Hello, I would like to get equation of two variables (x,y) from user's input. 'Enter the equation f(x,y): '. Then ask user for value of each variable 'Set value of x: ' and 'Set value of y: '. When all variables are in I want to evaluate the equation with variables. After this is it possible to countinue to work with the equation, to make a partial derivative evaluate that etc.? I'll be gratefull for any advice.
str1 = input('Enter and equation f(x,y); ','s');
x = input('Set the value of x: ');
y = input('Set the value of y: ');
% the rest was unsuccessful in my code

 Réponse acceptée

James Tursa
James Tursa le 11 Mar 2021
Modifié(e) : James Tursa le 11 Mar 2021
You can construct a function handle as follows:
f = str2func(['@(x,y)' str1]);
You can then use this function handle to evaluate at specific values of x and y.
If you want to take derivatives of this downstream in your code, you could create symbolic expressions to work with. E.g.,
syms x y
fxy = f(x,y)

6 commentaires

Patrik Matta
Patrik Matta le 11 Mar 2021
Modifié(e) : Patrik Matta le 11 Mar 2021
Thank you for help! This happened so far. I am a beginner in matlab.
format long; % this enables higher decimal accuracy
str1 = input('Enter f(x,y); ','s');
x = input('Set x: ');
y = input('Set y: ');
f = str2func(['@(x,y) ' str1]);
syms x y
F_xy = f(x,y);
disp(F_xy);
I don't fully understand how these variables and data types work so far, but I want to have my eqation and its value in two different variables, if that makes sense. Algorithm would be: Load equation, load x and y, put x and y into equation, load the result into other variable. Just like you would do on paper.
EDIT: I tried it without "syms x y" and now it gives me the numeric value. So if I understand correctly, as far as I want to work with the equation I use "syms x y" then I can differentiate etc. but if I want the equation to give me values I have to sort of "undo" syms?
I know, my explanation sounds dumb but you get the point.
It seems to work as I wanted it to. Except value of derivative with given x and y doesn't show up.
format long; % this enables higher decimal accuracy
str1 = input('Enter f(x,y); ','s');
x = input('Set x: ');
y = input('Set y: ');
f = str2func(['@(x,y) ' str1]);
F_xy = f(x,y);
syms x y
f_dx = diff(f,x);
disp(F_xy);
disp(f_dx); % this is only for check if derivative is correct
F_dx = f_dx(x,y);
disp(F_dx);
James Tursa
James Tursa le 11 Mar 2021
Modifié(e) : James Tursa le 11 Mar 2021
If you feed the function handle numeric values, then it will give you a numeric answer. If you feed the function handle symbolic variables, then it will give you a symbolic expression result. You can do both in your code, but you can't have the same variable mean two different things at the same time. E.g.,
str1 = input('Enter f(x,y); ','s');
f = str2func(['@(x,y) ' str1]);
% Use f with numeric variables x and y
x = input('Set x: ');
y = input('Set y: ');
disp('Numeric Results:');
fprintf('%s evaluated at x = %d and y = %d = %d\n',str1,x,y,f(x,y));
% Use f with symbolic variables x and y
disp('Symbolic Results:');
syms x y
F_xy = f(x,y);
disp('Expression:')
disp(F_xy);
disp('Derivative with respect to x:')
disp(diff(F_xy,x));
disp('Derivative with respect to y:')
disp(diff(F_xy,y));
Thank you this was very helpful. I'm starting to understand this. My (hopefully) last question is how could I evaluate for instance derivative with respect to x with a variable x given by user, if variable types are different.
James Tursa
James Tursa le 11 Mar 2021
Modifié(e) : James Tursa le 11 Mar 2021
If you want to work with both numeric and symbolic at the same time, then pick different variable names for them. E.g., let small case x and y be the symbolic and upper case X and Y be numeric. Then you can work with both at the same time. You can use the subs( ) function to get numeric values into a symbolic expression. E.g.,
str1 = input('Enter f(x,y); ','s');
f = str2func(['@(x,y) ' str1]);
% Use f with numeric variables X and Y (uppercase)
X = input('Set x: ');
Y = input('Set y: ');
disp('Numeric Results:');
fprintf('%s evaluated at x = %d and y = %d = %d\n',str1,X,Y,f(X,Y));
% Use f with symbolic variables x and y (lowercase)
disp('Symbolic Results:');
syms x y
F_xy = f(x,y);
disp('Expression:')
disp(F_xy);
disp('Derivative with respect to x:')
disp(diff(F_xy,x));
fprintf('Evaluated at x = %d and y = %d\n',X,Y);
subs(diff(F_xy,x),[x y],[X Y]);
disp('Derivative with respect to y:')
disp(diff(F_xy,y));
fprintf('Evaluated at x = %d and y = %d\n',X,Y);
subs(diff(F_xy,y),[x y],[X Y]);
Thank you, this was very helpful.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by