Using Heun's Method but Problem With Increment

조회 수: 2 (최근 30일)
Muhammad Hadyan Utoro
Muhammad Hadyan Utoro 2021년 8월 28일
댓글: Muhammad Hadyan Utoro 2021년 8월 30일
Hi everyone,
I'm really new to programming and try to use Heun's Method to this skydiving problem, but I kept getting this error, "Unable to perform assignment because the left and right sides have a different number of elements. Error in EulervsHeunStart (line 17)
vh(i) = vh;"
I know I have to use the increment to get the second slope (corrector). Here's my script:
while t<=tend
%save both velocities at time ti
vh(i) = vh;
ve(i) = ve;
tim(i)= t;
%good old Euler
a = g-c/m*ve.^2;
ve = ve+a*dt;
% x = x+v*dt;
%Heun: compute slope at ti
a = g-c/m*vh.^2;
%compute predicted velocity at ti+1
vh = vh+a*dt;
%compute second slope at v(ti+1)
a = g-c/m*vh(i).^2;
vh(i+1) = vh(i)+a*dt;
%compute average of both slopes
%use average slope for extrapolating v from ti to ti+1
%increments
t=t+dt;
i=i+1;
end
Thank you!
  댓글 수: 2
darova
darova 2021년 8월 29일
PLease atach the whole code
Muhammad Hadyan Utoro
Muhammad Hadyan Utoro 2021년 8월 29일
Hi darova,
please find the attachment for the whole code, but they're pretty much the same as I put on the question and 've' and 'vh' variables are for velocity using euler and velocity using heun respectively. I'd like to compare the error.
Many thanks

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

채택된 답변

darova
darova 2021년 8월 29일
I think you should use index (i) everywhere. vh variable has two elements after first iteration
You trying to assign two elements into one position in this line
vh(i) = vh;
  댓글 수: 1
Muhammad Hadyan Utoro
Muhammad Hadyan Utoro 2021년 8월 30일
I've used all index on every ve and vh, but it even said, "Unable to perform assignment because the left and right sides have a different number of elements.
Error in EulervsHeunStart (line 17)
vh(i) = vh;"
I don't understand how this works, but thanks again darova. I really appreciate your help

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by