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
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)
