I can't get input a trigonometric expression

1 visualización (últimos 30 días)
Merve Basak Uzun
Merve Basak Uzun el 25 de Mayo de 2022
Editada: Voss el 26 de Mayo de 2022
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.

Respuesta aceptada

Voss
Voss el 25 de Mayo de 2022
u = input('Enter the function to be integrated: ','s');
f = str2func(['@(x)' u]);
  2 comentarios
Merve Basak Uzun
Merve Basak Uzun el 26 de Mayo de 2022
Thank you so much!!
Voss
Voss el 26 de Mayo de 2022
Editada: Voss el 26 de Mayo de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by