Effacer les filtres
Effacer les filtres

How can I replace an inline function with a anonymous function?

8 vues (au cours des 30 derniers jours)
ElPerroVerde
ElPerroVerde le 20 Mai 2020
Modifié(e) : Rik le 20 Mai 2020
Usually when I used to work with inline functions I was able to call the formula from a variable. For example:
a=input('Insert a number: ');
formulaOne=['x.^',num2str(a)];
Example=inline(formulaOne,'x');
disp(feval(Example,10))
And the result was equal to 10^a. This is an example but I have a code that generates different formulas so I want to replace the inline function with an @() function.
Here's what I tried:
a=input('Insert a number: ');
formulaOne=['x.^',num2str(a)];
Example=@(x) formulaOne;
disp(Example(10))
I wanted to get the numerical value of the function but I get the text instead:
x.^2 %this is the result I'm getting.
So can I make it read the formula from formulaOne and evaluate it with an anonymous function instead of getting the formula itself?.
Pd. I don't have the Symbolic Math Toolbox instaled so I can't use syms or matlabFunction to solve it.

Réponses (1)

Rik
Rik le 20 Mai 2020
Modifié(e) : Rik le 20 Mai 2020
a=input('Insert a number: ');
Example=@(x) x.^a;
disp(Example(10))
Alternatively, you can use str2func to create a function handle from a char array.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by