# Solving a simple integral with input equation from user

조회 수: 2 (최근 30일)
jarly añasco 2020년 8월 3일
댓글: Walter Roberson 2020년 8월 3일
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)

댓글을 달려면 로그인하십시오.

### 채택된 답변

Rafael Hernandez-Walls 2020년 8월 3일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Walter Roberson 2020년 8월 3일
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.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Walter Roberson 2020년 8월 3일
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)

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by