查看单个帖子
旧 2019-11-25, 14:23   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
默认 Using Euler's method to graph in matlab

having some trouble with this code. My professor asked us to create a function "feuler.m" in matlab to solve the initial-value problem given by the differential equation u′(t) = (2+2t)e^t and the initial condition u(0) = 0 over the interval [0, 5] that uses (forward) Euler’s method to graph the exact solution along with the approximate solution.



The input should be: n, the number of subintervals into which the interval [0,5] should be divided.



The output should be a graph of the exact solution and the numerical solution and print the value of the maximum error between the true solution and the numerical solution.



Note that the exact solution is given by u(t) = 2tet.



So far I have written the code:



function myeuler(N)
t = linspace(0, 5, N+1)';
ua = zeros(N+1,1);
ue = 2*t.*exp(t);
h = 5/N;
A = zeros(N,N);
A(2:N,1:N-1) = -eye(N-1);
A= A + eye(N);
b = h*(2+2*t(1:N)).*exp(t(1:N));
b(1) = b(1) + ua(1);
ua(2:N+1) = A\b;
plot(t, ua, 'r', t, ue, 'g')
end


Unsure if this is right.



Thanks for any help!





More answer...
poster 当前离线   回复时引用此帖