MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Using Euler's method to graph in matlab (https://www.labfans.com/bbs/showthread.php?t=22123)

poster 2019-11-25 14:23

Using Euler's method to graph in matlab
 
<p>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.</p>

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

<p>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.</p>

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

<p>So far I have written the code:</p>

<pre><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
</code></pre>

<p>Unsure if this is right.</p>

<p>Thanks for any help!</p>



[url=https://stackoverflow.com/questions/59026136/using-eulers-method-to-graph-in-matlab]More answer...[/url]


所有时间均为北京时间。现在的时间是 19:43

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.