I can't get input a trigonometric expression

clc;
b = input("Enter the initial limit value of the definite integral: ");
a = input("Enter the limit value that terminates the definite integral: ");
n = input("Enter the value of N: ");
h=(b-a)/n;
sum=0;
u = input("Enter the function to be integrated: ");
f=@(x) u;
for i=1:1:n-1
sum= sum + f(a+i*h);
end
result = h/2*(f(a)+f(b)+2*sum);
fprintf('%f',result);
It gives an error because of the "u" input. It throws an error when "u" writes a trigonometric expression to the input. I need your help, thanks.

 Accepted Answer

u = input('Enter the function to be integrated: ','s');
f = str2func(['@(x)' u]);

2 Comments

Voss
Voss on 26 May 2022
Edited: Voss on 26 May 2022
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 25 May 2022

Edited:

on 26 May 2022

Community Treasure Hunt

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

Start Hunting!