Trapezodial Method using while loop

Hi for my numerics class i have an assignment, which is supposed to be quite easy but I can't seem to find my mistake. We have to solve the integral of sin(x) within 0 and pi using the trapezodial method and double the interval until the solution is close enough to the true value 2. So far my code looks like this, and i'm not supposed to use trapz.
clear
f=@(x) sin(x);
a=0;
b=pi;
n=1;
h=(b-a)/n;
tol=10^-6;
I=h*s
s=0.5*(f(a)+f(b))
i=0
while abs(2-I)>=tol
n=n*2;
a=b-(b/n);
b=b/n;
h(n)=((b*(n-1)/n)-(a/n))/n;
s(n)=0.5*(f(a)+f(b));
I=sum(h(n)*s(n));
i=i+1;
end
I
but it takes a really long time and gives me the following error:
Requested 1073741824x1 (8.0GB) array exceeds maximum
array size preference. Creation of arrays greater
than this limit may take a long time and cause MATLAB
to become unresponsive. See array size limit or
preference panel for more information.
Error in NumMeth3 (line 18)
h(n)=((b*(n-1)/n)-(a/n))/n;
I don't know why this gets so 'big'. I've tried a lot but nothing seems to work, so I'd love some help!
Thanks in advance.

댓글 수: 5

Rik
Rik 2020년 5월 18일
If you had put a breakpoint on one of the first line to step through your code line by line, you would have noticed that h and s are arrays that are growing in size every iteration. It doesn't seem necessary to save them as array, although removing the (n) will probably not solve the underlying problem.
I think you have to take a look again at the algorithm and try to put the steps in words first. Then paste it into Matlab as comments. Only after that start writing the code. That way we can more easily see where your code deviates from your intentions.
clear
f=@(x) sin(x); %Function for integration
a=0; %Lower limit
b=pi; %Upper limit
n=1; %Number of intervals
h=(b-a)/n; %Size of each interval
tol=10^-6; %Tolerance for solution
s=0.5*(f(a)+f(b))%Second part of the equation for Trapezodial method
I=h*s %Putting both parts together by multiplying
i=0 %How often is loop repeated until criteria is met
while abs(2-I)>=tol %repeat until the solution is within the tolerance
n=n*2; %double the intervals for every time loop is repeated
a=b-(b*(n-1)/n); %new lower limit
b=b-b/n; %new upper limit
h=(b-a)/n; %size of the new interval
s=0.5*(f(a)+f(b));%Second part of equation for new limits
I=sum(h*s); %Summing the solutions for each interval
i=i+1; %Counting iterations
end
I
Maybe it makes more sense what I'm trying to do now? Also I'm aware that the upper limit b=pi is not included anymore, but I can't think of a way to change it properly.
I took away the (n) and now it's not giving me an error anymore but also no solution. If I run it it now gives me NaN as soution for I, s, h and a
Sorry but I'm really new to all this ..
Rik
Rik 2020년 5월 18일
You annotated your code, which is good, but the wrong way around. You should write the comments first, and then the code.
You need to split the curve into trapezoids. How can you divide the range of 0 to pi into sections? How would you create the arrays with the corners of the trapezoids? How would you calculate the area of each trapezoid separately?
LauraB
LauraB 2020년 5월 18일
Okay, i guess this needs a whole different approach then..
I wrote down the first few iterations on paper and then tried to 'translate' it into code, but it seems I haven't worked thoroughly enough. I'm gonna try it your way this time :)
One hint that should help dividing the range 0 to pi in segments:
linspace(0,pi,n)

댓글을 달려면 로그인하십시오.

답변 (1개)

David Hill
David Hill 2020년 5월 18일

0 개 추천

f=@(x) sin(x);
tol=1e-6;
x=linspace(0,pi,2);
A=sum(movsum(f(x),2,'Endpoints','discard')/2.*diff(x));
n=2;
while abs(2-A)>=tol
x=linspace(0,pi,n+1);
A=sum(movsum(f(x),2,'Endpoints','discard')/2.*diff(x));
n=2*n;
end

댓글 수: 2

Rik
Rik 2020년 5월 18일
Since this is homework I wouldn't encourage posting complete working examples. Also, without comments it isn't immediately obvious that this is an implementation of the trapezoidal method.
LauraB
LauraB 2020년 5월 18일
I agree, but it sometimes helps to take in a different view to find your own mistakes. So thank you anyway!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 5월 18일

댓글:

Rik
2020년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by