how take a mathematical function from user
Mostra commenti meno recenti
How can i take F and G from the user to use them in a function if i have F=@(x,y) x./y;
and G=@(x) sqrt(x.^2+1); ?
4 Commenti
Dyuman Joshi
il 8 Mag 2021
You mean, you want the user to input x and y and get the F and G values?
Jonas
il 8 Mag 2021
if you really want to be able to let the user enter matlab code (a function), then you may use feval().
Ahmed Amin
il 8 Mag 2021
Ahmed Amin
il 8 Mag 2021
Risposte (1)
What do you mean when you say you want "the user to enter" the function in your comment?
Do you want the user to have to pass some thing into your function that lets your function call a function of their choosing? If so you want your user to define a function handle (which could be an anonymous function.)
f1 = @(x, y) x./y; % anonymous function
z1 = integral2(f1, 0, 1, 1, 2) % integral2 will call the function f with inputs of its choice
f2 = @sin; % regular function handle
z2 = integral(f2, 0, pi)
Or do you want to have the user enter the text of a function in say an edit box in a dialog or UI? In that case you should use str2func.
s = 'x./y';
f3 = str2func(['@(x, y)' s]); % make an anonymous function which is a function handle
z3 = integral2(f3, 0, 1, 1, 2)
% or
f4 = str2func('sin'); % make a regular function handle
z4 = integral(f4, 0, pi)
Or did you have a different meaning in mind? In this case, please explain what you want to do in more detail.
Categorie
Scopri di più su Function Handles in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
