I am trying to ask the user for a function of x that can be used subsequently in the code. I tried to use str2func but that is throwing an error

1 visualización (últimos 30 días)
str = input('Give a function to minimize in x: ','s') ;
f = "@x" +str; %%str2func(['@x '.*str]) ;
f = str2func(f);
%%Initializations
gr=double(0.5*(sqrt(5)-1)); % golden ratio
iter=0; %iteration counter
%%Initial Calculations
x1=l+(1-gr)*(u-l);
x2=l+gr*(u-l);
y1=f(l);
y2=f(u);

Respuesta aceptada

Matt J
Matt J el 1 de Ag. de 2021
f = "@(x)" +str;

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by