Euler Method without using ODE solvers such as ode45
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I am trying to write a code that will solve a first order differential equation using Euler's method. I do not want to use an ode solver but rather would like to use numerical methods which allow me to calculate slope (k1, k2 values, etc). I am given an equation with two different step values. I am not sure how to begin to write this in MATLAB. I have solved the equation by hand and am now trying to write a code that solves that equation.
The equation to be used is y’ +2y = 2 – e -4t.
y(0) = 1, which has an exact solution:
y(t) = 1 + 0.5 e -4t - 0.5 e -2t .
I did the following to solve numerically: 1+(0.5e^-4t) - (0.5e^-2(0)) y_n+1=1+0.1 and y_n+1=1+1(.001)
The step sizes are 0.1 and 0.001. (so my h in this example). The t values range from 0.1 to 5.0.
Any help is appreciated even if its an example pseudocode. Thank you
채택된 답변
Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))
To write this: EDIT
y = 1; % y at t = 0
h = 0.001;
t_final = 0.1;
t = 0;
while (t < t_final)
y = y +h*(- 2*y+ 2 - exp(-4*t));
t = t + h;
end
댓글 수: 11
My mistake the equation should be: y’ +2y = 2 – e^(-4t). Meaning that e is raised to the negative 4t.
y(t) = 1 + 0.5 e^(-4t) - 0.5 e^ (-2t)
Sorry for the confusion
The following is the code that I am trying to implement. I am unsure of the total steps since t ranges from 0 to 5 in increments of 0.1. ANy help is again appreciated. Thank you %Rearrange your equation to represent the following: y'= 2-e^-4*t-2*y; %define your f(t,y) f(t,y)=2-e^-4*t-2*y; %define initial t and initial y; define step size(h) and number of steps(n) t0=0; y0=1; h=0.1; n= %Write for loop for i=1:numel(n)-1 m=f(t0,y0); y=y0 +h*m; t1=t0+ht0; Print t1,y1; t0=t1; y0=y1; end
I have corrected the code accordingly. Your code will be similar to the one I wrote above.
Can you format your code with code attributes so that I can see it.
y'= 2-e^-4*t-2*y;
f(t,y)=2-e^-4*t-2*y;
t0=0; y0=1; h=0.1; n=
for i=1:numel(n)-1
m=f(t0,y0);
y=y0 +h*m;
t1=t0+ht0;
Print t1,y1;
t0=t1;
y0=y1;
end
I am not sure the number of total steps(n) since my t values go from 0 to 5.0 with a step size of 0.1
Was using this code I found online: define f(t,y)
input t0 and y0.
input step size, h and the number of steps, n.
for j from 1 to n do m = f (t0, y0) y1 = y0 + h*m t1 = t0 + h Print t1 and y1 t0 = t1 y0 = y1 end
Okay if you want to follow this way, then try this:
f = @(t,y) (2 - exp(-4*t) - 2*y);
h = 0.1; % Define Step Size
t_final = 5;
t = 0:h:t_final;
y = zeros(1,numel(t));
y(1) = 1; % y0
% You know the value a t = 0, thats why you'll state with t = h i.e. i = 2
for i = 2:numel(t)
y(i) = y(i-1) + h*f(t(i-1),y(i-1));
disp([t(i) y(i)]);
end
Thank you so much. And then for my step size of 0.001 I would just change the h variable in said above code to 0.001
Yup !
How can you be unsure of the total number of steps if you know the stepsize, and you know how far it must go? Surely division is the proper tool here. 5/0.1
Amit,
How did you determine the approximation for the equation?
I have a similar problem I am trying to solve only I have y' = ry(1-y/K) where r = 1, K = 10, and yo = 1.
I understand the code that you have written but I'm unsure of how to alter my function.
"Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))"
@Erin: Open a new Question with your specific problem, what code you have written so far, and what specific things you need help with.
@James I did but I haven't been responded to yet :(
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
