trying to use heun's method to solve an ode

조회 수: 31 (최근 30일)
Anas Gharsa
Anas Gharsa 2022년 1월 27일
편집: Torsten 2022년 1월 28일
I am trying for days to solve an ode using heun's method for a project but i always keep getting an error message!!!this is what I tried :
f = @(t,y)(m*yn' + c*yn + k*yo - F0*sin(w*t));
a = 0;
b = 10;
n = 100;
h = (b-a)/n;
T = a:h:b;
F0 = 0;
w = 0;
m = 4;
k = 10;
c = 0.2;
y0 = 4;
[t,y] = myHeun(y0,a,b,f,n);
plot(t,y,'k')
  댓글 수: 5
Anas Gharsa
Anas Gharsa 2022년 1월 27일
I even tried something like this :
f = @(t,y)(m*yn' + c*yn + k*yo - F0*sin(w*t));
a = 0;
b = 10;
n = 100;
h = (b-a)/n;
T = a:h:b;
F0 = 0;
w = 0;
m = 4;
k = 10;
c = 0.2;
y0 = 4;
h=(b-a)/n; %h=(b-a)/n
t=a:h:b;
u=y0*ones(1,n+1); %initial condition
for i=1:n
fi=feval(f,t(i),u(i)); %evaluation of f at t_i, u_i
u0=u(i)+h*fi; %application of Euler method to obtain u_{i+1}^0
u(i+1)=u(i)+h*(fi+feval(f,t(i+1),u0))/2; %application of Heun method
end
plot(t,y,'k')
and i get this error message:
Error in ex7>@(t,y)(m*yn'+c*yn+k*yo-F0*sin(w*t)) (line 3)
f = @(t,y)(m*yn' + c*yn + k*yo - F0*sin(w*t));
Error in ex7 (line 21)
fi=feval(f,t(i),u(i)); %evaluation of f at t_i, u_i
Torsten
Torsten 2022년 1월 27일
What is yo ?

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

채택된 답변

VBBV
VBBV 2022년 1월 27일
편집: VBBV 2022년 1월 27일
a = 0;
b = 10;
n = 100;
h = (b-a)/n;
% T = a:h:b;
F0 = 0;
w = 0;
m = 4;
k = 10;
c = 0.2;
y0 = 4;
h=(b-a)/n; %h=(b-a)/n
t=a:h:b;
u=y0*ones(1,n+1);
f = @(t,y)(m*y + c*y + k*y0 - F0*sin(w*t));%initial condition
for i=1:n-1
fi=feval(f,t(i),u(i)); %evaluation of f at t_i, u_i
u0=u(i)+h*fi; %application of Euler method to obtain u_{i+1}^0
u(i+1)=u(i)+h*(fi+feval(f,t(i+1),u0))/2; %application of Heun method
end
plot(t,u,'k')
  댓글 수: 8
Torsten
Torsten 2022년 1월 27일
You wrote you want to learn Matlab, and this is fine.
But since you got your problems as exercises for your homework, you should be able to state them properly.
And everybody in the forum is willing to help if you show that you spent some effort to solve the problem on your own. But did you really spend this effort ? Since the Heun program can't be written on your own if you don't know how to use it.
Anas Gharsa
Anas Gharsa 2022년 1월 27일
Dear Torsten, you got right to say that since you really don't know how much time i spend on this. I am not asking to risolve the exercises for me I am just asking some advices that would help me and may be guidance to the right direction!! and like i said it befor i am new to matlab and i am doing my best to learn it !! and for the Heun algorithm i did what i could after reading thounsands of pages that speaks of numerical solutions and reading many others scripts!! you can say everything you want but you can't tell me that i did nothing to try it by my self
by the way thank you for trying to help and thank you so much VBBV

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by