I managed to create the rectangle and trapezium strips, but stuck on the parabola strips for Simpson's Rule like the one shown below.
In this code, the user has to input the strip width, function, limits,
Here's my code for the RECTANGLE STRIPS
clf, hold on;
plot([a b], [0 0], 'k' ), plot([0 0], [min( y_x(x) ) max( y_x(x))], 'k')
xlabel('x', 'FontWeight', 'bold'), ylabel('y(x)', 'FontWeight', 'bold')
title(['Numeric integration through Rectangle Rule of y(x)=' , y_xs , ' with ', num2str(n), ' slices ||| Result is ', num2str(S_r) '.'] , 'FontWeight', 'bold')
for x=a:dx:(b-dx);
y_x(x);
left = x; right = x+dx; bottom = 0; top = y_x(x);
X = [left left right right]; Y = [bottom top top bottom];
fill(X,Y, 'b', 'FaceAlpha', 0.3)
end
x = a:dx/100:b;
plot(x, y_x(x), 'k')
Here's my code for the TRAPEZIUM STRIPS:
clf, hold on;
plot(x, y_x(x), 'k--')
plot([a b], [0 0], 'k'), plot([0 0], [min( y_x(x) ) max( y_x(x))], 'k')
xlabel('x', 'FontWeight', 'bold'), ylabel('y(x)', 'FontWeight', 'bold')
title(['Numeric integration through Trapezium Rule of y(x)=' , y_xs , ' with ', num2str(n), ' slices ||| Result is ', num2str(S_t) '.'] , 'FontWeight', 'bold')
for x=a:dx:(b-dx);
y_x(x);
left = x; right = x+dx; bottom = 0; top1 = y_x(x); top2 = y_x(x+dx);
X = [left left right right]; Y = [bottom top1 top2 bottom];
fill(X,Y, 'b', 'FaceAlpha', 0.3)
end
x = a:dx/100:b;
plot(x, y_x(x), 'b')
Comments on how to optimise and improve brevity this code would also be appreciated! Cheers
6 Comments
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139420
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139420
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139705
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139705
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139768
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139768
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139806
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139806
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139809
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_139809
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_145704
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/68845-simpson-s-rule-illustration-how-to-create-those-parabolas#comment_145704
Sign in to comment.