How can i improve my euler code? Code is attached ... I believe i have to improve it by using trapezoids instead of rectangles..How do i do this ? Can someone help

 채택된 답변

Jim Riggs
Jim Riggs 2018년 2월 20일

0 개 추천

For trapezoidal integration, you use the average of two consecutive evaluations. This could be done by simply adding one line of code after the Euler calculation;
y(:,k+1) = (y(:,k+1) + y(:,k))*h/2

댓글 수: 2

Raiven Balderas
Raiven Balderas 2018년 2월 21일
so i add your suggestion directly below "y(:,k+1)=y(:,k)+h*func(t(k),y(:,k)); " ?
Jim Riggs
Jim Riggs 2018년 2월 21일
편집: Jim Riggs 2018년 2월 21일
Perhaps this will be more clear:
A = func(t(k), y(:,k) );
B = func(t(k+1),y(:,k+1));
y(:,k+1) = y(:,k) + (A + B) * h/2;
Compare this with the Euler method:
A = func(t(k), y(:,k) );
y(:,k+1) = y(:,k) + A * h;

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2018년 2월 20일

편집:

2018년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by