Simpsons 1/3 rule using n =10 , 100, 1000
1 次查看(过去 30 天)
显示 更早的评论
i cant get my code to work. im trying to implement the simpsons one-third rule to get the value of the certain function using n =10, 100, 1000. Any Help i would prefer to use the for loops.
%% Parameters
P = 5000; %lb
a= 0; %lower bound
b = 10; %upperbound
n = [10 100 1000]; % step size
f = @(x) P / ((30e6)*(1-0.01*x-0.0005*x.^2)*2*exp(-0.1*x)); %function
%% Simpsons one-third rule
for j = 1:length(n)
h(j) = (b-a)/(n(j));
f_even = 0;
for i = 2:2:n-2 %even
x = a+i *h(j);
f_even = f_even + f(x);
end
f_odd = 0;
for i = 1:2:n-1 %odd
x = a+i *h(j);
f_odd = f_odd + f(x);
end
I(j) = (h(j)/3) * (f(0) + 4* f_odd + 2*f_even +f(10)); %one-third rule
end
2 个评论
Tommy
2020-4-17
One thing I noticed:
for i = 2:2:n-2 %even
Here, use
for i = 2:2:n(j)-2 %even
instead.
Same with
for i = 1:2:n-1 %odd
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!