Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Trapetsmetoden: noggrannhetsording 2(h), felet blir en fjärdedel
- % enligt richardsons extrapolation blir NO = 2
- % simpsonsmetoden kan fungera bättre. 4 (h^4),
- % (felet blir 1/16 så stort)
- % sammanfattningsvis har trapetsmetoden O(n^2),
- % vilket gör att den är bättre om h>1
- % om det är under 1 är således simpsons bättre.
- % integral_(-5)^5 e^(-x^2) dx = sqrt(π) erf(5)≈1.77245 (gauss-integral)
- %
- clc; clear; format long;
- f = @(x) exp(-x*x) * max(generator(x+30, 0));
- h = 1;
- a = -5;
- b = 5;
- integral = (f(a) + f(b))/2;
- for x = a:h:b
- integral = integral + f(x);
- end
- integral = h * integral
- % integral =
- % 1.904283544576528e+04
- clc;clear
- % https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/12Differentiation/richardson/matlab.html
- f = @(x) sqrt(x+2)
- x = 1;
- h = 0.05;
- eps_step = 0.00001;
- R(1, 1) = (f(x + h) - f(x - h))/(2*h);
- h = h/2;
- R(2, 1) = (f(x + h) - f(x - h))/(2*h);
- for j=1:1
- R(2, j + 1) = (4^1*R(2, 1) - R(1, 1))/(4^1 - 1);
- end
- R
- % for i=1:100
- % h = h/2;
- %
- % R(i + 1, 1) = (f(x + h) - f(x - h))/(2*h);
- %
- % for j=1:i
- % R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
- % end
- %
- % if ( abs( R(i + 1, i + 1) - R(i, i) ) < eps_step )
- % break;
- % elseif ( i == 100 )
- % error( 'Richardson extrapolation failed to converge' );
- % end
- % end
- R
- % %simpsons
- % clc; clear; format long;
- % f = @(x) exp(-x*x) * max(generator(x+30, 0));
- %
- % a = -5;
- % b = 5;
- % n = 4;
- % h = (b - a) / n;
- % s = f(a) + f(b);
- %
- % for i = 1:2:n
- % s = s + (4 * f(a + i * h));
- % end
- %
- % for i = 2:2:n-1
- % s = s +( 2 * f(a + i * h));
- % end
- %
- % integral = s * h / 3
- %
- % %ordning 2
- %
- % %Approximation by Simpson's rule from a to b
- % c=(a+b)/2.0
- % h=abs(b-a)/6.0
- %
- % for x = a:1:b
- % integral = integral + f(x);
- % end
- % integral = h*(f(a)+4.0*f(c)+f(b))
- %
- % %ordning 4
- %
- % % integral =
- % % 2.387175473218235e+04
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement