Why do I get the wrong value when I increase N?
Afficher commentaires plus anciens
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
Réponses (1)
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.'
Exact_num = integral(fun,a,b)
Exact = 10/pi*(cos(pi*a/10) - cos(pi*b/10))
2 commentaires
Maddy Taylor
le 2 Août 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
Exact = 10/pi*(cos(pi*x(1)/10) - cos(pi*x(end)/10))
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
Catégories
En savoir plus sur Graphics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!