Solving a simple integral with input equation from user

4 visualizaciones (últimos 30 días)
I am using this code to enter an equation and solve a simple integral
str = input('Enter an equation in x: ','s') ;
f = function_handle.empty;
f = eval(['@()', str]);
x = f();
xmin= input('Lower limit of x: ')
xmax= input('Upper limit of x: ')
int(x, xmin, xmax)
But I get this error
Undefined function 'int' for input arguments of type 'double'.
Error in Untitled5 (line 7)
int(x,xmin, xmax)

Respuesta aceptada

Rafael Hernandez-Walls
Rafael Hernandez-Walls el 3 de Ag. de 2020
syms x
str = input('Enter an equation in x: ','s') ;
f = function_handle.empty;
f = eval(['@(x)', str]);
%x = f();
xmin= input('Lower limit of x: ')
xmax= input('Upper limit of x: ')
int(f,x, xmin, xmax)
  1 comentario
Walter Roberson
Walter Roberson el 3 de Ag. de 2020
Using eval() is not recommended, and is not necessary. If you were going to generate an anonymous function from a character vector, then use str2func() . But considering that that symbolic integration int() is being used, it does not make sense to convert to an anonymous function: it makes more sense to convert to a symbolic expression or possibly symbolic function.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 3 de Ag. de 2020
syms x
str = input('Enter an equation in x: ','s') ;
f = str2sym(str) ;
xmin= input('Lower limit of x: ')
xmax= input('Upper limit of x: ')
int(f,x, xmin, xmax)

Categorías

Más información sobre Numbers and Precision en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by