How can I plot the Simpson's rule function?

10 views (last 30 days)
Shane
Shane on 24 Apr 2014
I have been set the task to evaluate and plot the graphs representing the Rectangle Rule, Trapezium Rule and Simpson's Rule. I have managed to plot the first two, but the Simpson's Rule has me baffled.
My code begins with the calculations of each approximation and then goes on to plot what they represent.
I have attached the graphs that have so far been plotted...
Here is my Code:
%Default program
for run = menu('Run default program?','Yes','No');
if run == 1;
x0 = -10;
x1 = 10;
n = 20;
h = (x1 - x0)/n; %State value of h (strip width)
x_val = x0:h:x1; %Define x_val
y_func = x_val.^3; %Define y_func
x_vala = x0:h/10:x1; %Define accurate x_vala
y_fa = x_vala.^3; %Define accurate y_func
elseif run == 2;
x0 = input('please input x0: '); %Input values of x0, x1 and n
x1 = input('please input x1: ');
n = input('please input n: ');
h = (x1 - x0)/n; %State value of h
x_val = x0:h:x1; %Define x_val
x_vala = x0:h/10:x1; %Define x_vala
y_func = input('please input y_func as a function of x_val: '); %Input value of y_func
y_fa = input('please input y_func as a function of x_vala: '); %Input value of y_fa
end
end
total = 0; % Define totals
ttotal = 0;
% Rectangle Rule calc
for r = 1:n;
Ar = (h * y_func(r));
total = total + Ar;
end
disp (['Rectangle Rule: ' num2str(total)]) %display total
% Trapezium Rule calc
for t = 1:n;
At = ((h * (y_func(t) + y_func(t+1)))) / 2;
ttotal = ttotal + At;
end
disp (['Trapezium Rule: ' num2str(ttotal)]) %display total
% Simpson's Rule calc
% Define Simpson even and odd sections
simpo = 0; %odd
simpe = 0; %even
for s = 2:2:n;
simpe = simpe + (4 * y_func(s));
end
for s = 3:2:n-1;
simpo = simpo + (2 * y_func(s));
end
totals = h/3 * (y_func(1) + simpo + simpe + y_func(n));
disp (['Simpson Rule: ' num2str(totals)]) %display total
%Plot Graph
subplot(1,3,1)
%rectangle rule graph and plot
for r = 1:n;
try
hold on
plot([x_val(r), x_val(r+1), x_val(r+1), x_val(r), x_val(r)], [0, 0, y_func(r), y_func(r), 0],'r-') %plot rectangle rule
catch
continue
end
hold off
end
hold on
plot(x_vala,y_fa,'k-','linewidth', 2) %plot accurate function
grid on
title('Rectangle Rule')
xlabel('x_val')
ylabel('y_func')
text(0,x1, num2str(total),'backgroundcolor', [1 1 1],'color', [0 0 0],'edgecolor', [0 0 0])
hold off
%Trapezium rule graph and plot
subplot(1,3,2)
hold on
plot(x_vala,y_fa,'k-','linewidth', 2) %plot accurate function
grid on
title('Trapezium Rule')
xlabel('x-val')
ylabel('y-func')
text(0,x1, num2str(ttotal),'backgroundcolor', [1 1 1],'color', [0 0 0],'edgecolor', [0 0 0])
hold off
for t = 1:n;
try
hold on
plot([x_val(t), x_val(t+1), x_val(t+1), x_val(t), x_val(t)], [0, 0, y_func(t+1), y_func(t), 0],'b-') %plot trapezium rule
catch
continue
end
hold off
end
% simpson's rule graph
subplot(1,3,3)
hold on
plot(x_vala,y_fa,'k-','linewidth', 2) %plot accurate function
grid on
title('Simpsons Rule')
xlabel('x-val')
ylabel('y-func')
text(0,x1, num2str(ttotal),'backgroundcolor', [1 1 1],'color', [0 0 0],'edgecolor', [0 0 0])
hold off

Answers (0)

Community Treasure Hunt

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

Start Hunting!