Question:
Consider the initial value problem: dy/dt= e−t − 3y, y(−1) = 0.
Part a: Use the MATLAB program myeuler.m from Chapter 8 to compute the Euler Method approximation to y(t) with step size h = 0.5 and n = 4 steps. The program will generate a list of ordered pairs (ti, yi). Use plot to graph the piecewise linear function connecting the points (ti, yi). Repeat with h = 0.2 and n = 10.
Code:
f = @(t,y) exp(-t)-3*y;
[t, y] = myeuler(f,-1,0,1,4);
plot(t,y)
[t, y] = myeuler(f,-1,0,1,10)
plot(t,y)

5 件のコメント

James Tursa
James Tursa 2015 年 10 月 7 日
You need to show us the myeuler.m code so we know what the arguments are supposed to be.
Bob
Bob 2015 年 10 月 12 日
編集済み: Walter Roberson 2015 年 10 月 12 日
Here:
function [t, y] = myeuler(f, tinit, yinit, b, n)
%MYEULER Euler approximation for initial value problem,
% dy/dt = f(t, y), y(tinit) = yinit.
% Approximations y(1),..., y(n + 1) are calculated at
% the n + 1 points t(1),..., t(n + 1) in the interval
% [tinit, b]. The right-hand side of the differential
% equation is defined as an anonymous function f.
% Calculation of h from tinit, b, and n.
h = (b - tinit)/n;
% Initialize t and y as length n + 1 column vectors.
t = zeros(n + 1, 1);
y = zeros(n + 1, 1);
% Calculation of points t(i) and the corresponding
% approximate values y(i) from the Euler Method formula.
t(1) = tinit;
y(1) = yinit;
for i = 1:n
t(i + 1) = t(i) + h;
y(i + 1) = y(i) + h*f(t(i), y(i));
end
Walter Roberson
Walter Roberson 2015 年 10 月 12 日
Bob
Bob 2015 年 10 月 13 日
I keep getting an error:
Error in Project_2 (line 94) [t, y] = myeuler(f,-1,0,1,4);
Bob
Bob 2015 年 10 月 13 日
Need help with this asap!
Thanks

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 13 日

0 投票

Your code executes without error when I try it without change.
Make sure that the code you posted for myeuler is saved in myeuler.m and that the directory it is saved in is on your MATLAB path (for example if you are cd'd to the same directory)

5 件のコメント

Bob
Bob 2015 年 10 月 13 日
編集済み: Bob 2015 年 10 月 13 日
Could you please post a screenshot to how you are ruining the code because I am still getting an error for this code:
f = @(t,y) exp(-t)-3*y;
[t, y] = myeuler(f,-1,0,1,4);
plot(t,y)
[t, y] = myeuler(f,-1,0,1,10)
plot(t,y)
or past the code that you entered.
Walter Roberson
Walter Roberson 2015 年 10 月 13 日
>> f = @(t,y) exp(-t)-3*y;
[t, y] = myeuler(f,-1,0,1,4);
plot(t,y)
[t, y] = myeuler(f,-1,0,1,10)
plot(t,y)
t =
-1
-0.8
-0.6
-0.4
-0.2
-5.55111512312578e-17
0.2
0.4
0.6
0.8
1
y =
0
0.543656365691809
0.662570731975217
0.629452052868189
0.55014576067553
0.464338855902246
0.385735542360898
0.318040367559956
0.26128015623111
0.214274389711249
0.175575548707944
>> dbtype myeuler.m
1 function [t, y] = myeuler(f, tinit, yinit, b, n)
2
3 %MYEULER Euler approximation for initial value problem,
4
5 % dy/dt = f(t, y), y(tinit) = yinit.
6
7 % Approximations y(1),..., y(n + 1) are calculated at
8
9 % the n + 1 points t(1),..., t(n + 1) in the interval
10
11 % [tinit, b]. The right-hand side of the differential
12
13 % equation is defined as an anonymous function f.
14
15 % Calculation of h from tinit, b, and n.
16
17 h = (b - tinit)/n;
18
19 % Initialize t and y as length n + 1 column vectors.
20
21 t = zeros(n + 1, 1);
22
23 y = zeros(n + 1, 1);
24
25 % Calculation of points t(i) and the corresponding
26
27 % approximate values y(i) from the Euler Method formula.
28
29 t(1) = tinit;
30
31 y(1) = yinit;
32
33 for i = 1:n
34
35 t(i + 1) = t(i) + h;
36
37 y(i + 1) = y(i) + h*f(t(i), y(i));
38
39 end
Walter Roberson
Walter Roberson 2015 年 10 月 13 日
編集済み: Walter Roberson 2015 年 10 月 13 日
Literally no changes to your code. Note even deleting extra spaces.
You need to post the exact error message -- everything that shows up in red. And check
which -all myeuler
Bob
Bob 2015 年 10 月 13 日
編集済み: Bob 2015 年 10 月 13 日
Now I am getting this:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
Bob
Bob 2015 年 10 月 13 日
編集済み: Bob 2015 年 10 月 13 日
What is which -all myeuler? Should I have that somewhere in the code?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

Bob
2015 年 10 月 7 日

編集済み:

Bob
2015 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by