Why do I get the wrong value when I increase N?

How can I use the below equation for the function sin(pi*x/10) for epsilon=[ -sqrt(3/5) 0 sqrt(3/5)] and w=[5/9 8/9 5/9]?
This is my code so far. When I increase N to 3 I get the wrong answer.
epsilon = [-sqrt(3/5) 0 sqrt(3/5)]; % absciassas values for 3-point (n=3)
w = [5/9 8/9 5/9]; % weight factors for 3-point (n=3)
N = 2; % Number of segments
a = 0; % Lower Boundary
b = 10; % Upper Boundary
h = (b-a)/2/N; % Width of segment
x = h*epsilon + (b+a)/2/N; % Funtion evaluation point
f = sin(pi*x/10); % Function
Gauss = h*(sum(w.*f)*N) % Answer

Respuestas (1)

Torsten
Torsten el 2 de Ag. de 2022
Editada: Torsten el 2 de Ag. de 2022
a = 0; % Lower Boundary
b = 10; % Upper Boundary
fun = @(x)sin(pi*x/10); % Function
zeta = [-sqrt(3/5) 0 sqrt(3/5)]; % absciassas values for 3-point (n=3)
w = [5/9 8/9 5/9]; % weight factors for 3-point (n=3)
x = (a+b)/2 + zeta*(b-a)/2;
f = fun(x);
format long
Gauss = (b-a)/2*w*f.'
Gauss =
6.370618772999813
Exact_num = integral(fun,a,b)
Exact_num =
6.366197723675814
Exact = 10/pi*(cos(pi*a/10) - cos(pi*b/10))
Exact =
6.366197723675814

2 comentarios

Thank you. How can I apply this when the domain for the integral is diveded into multiple segments. Eg. N=2 (domain divided into 2 segements), For the first segment, the lower and upper limits of the integral are 0 and 5, respectively. For the second segment, the lower and upper limits of the integral are 5 and 10, respectively.
Torsten
Torsten el 2 de Ag. de 2022
Editada: Torsten el 2 de Ag. de 2022
x = 0:0.1:10;
f = @(x)sin(pi*x/10); % Function
int_value = 0.0;
for i = 1:numel(x)-1
a = x(i);
b = x(i+1);
int_value = int_value + gauss(a,b,f);
end
format long
int_value
int_value =
6.366197723675818
Exact = 10/pi*(cos(pi*x(1)/10) - cos(pi*x(end)/10))
Exact =
6.366197723675814
function int_value = gauss(a,b,f)
zeta = [-sqrt(3/5) 0 sqrt(3/5)]; % absciassas values for 3-point (n=3)
w = [5/9 8/9 5/9]; % weight factors for 3-point (n=3)
x = (a+b)/2 + zeta*(b-a)/2;
int_value = (b-a)/2*w*f(x).';
end

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Ag. de 2022

Editada:

el 2 de Ag. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by