Question

1. Consider the following differential equation. ag = ty, y(0)=1. dt (a) Use Eulers Method with At = .1 to approximate y(1).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB Script:

close all
clear
clc

% Given
f = @(t,y) t^2 * y; % Given ODE
t0 = 0; tf = 1; % Intervals of x
y0 = 1; % Initial condition

fprintf('Part (a)\n----------------------------------\n')
h1 = 0.1; % Step Size 1
x1 = t0:h1:tf;
y1 = my_euler(t0, y0, tf, h1, f);
fprintf('For h = 0.1, y(1) = %.6f\n', y1(end))

fprintf('\nPart (b)\n----------------------------------\n')
h2 = 0.05; % Step Size 2
x2 = t0:h2:tf;
y2 = my_euler(t0, y0, tf, h2, f);
fprintf('For h = 0.05, y(1) = %.6f\n', y2(end))

fprintf('\nPart (c)\n----------------------------------\n')
% Exact Solution
syms y(t)
ODE = diff(y,t) == t^2 * y; % Given ODE
cond = y(0) == 1; % Initial condition
y_sol = dsolve(ODE, cond); % Solver
fprintf('For h = 0.1, Error = %.6f\n', abs(subs(y_sol, 1) - y1(end)))
fprintf('For h = 0.05, Error = %.6f\n', abs(subs(y_sol, 1) - y2(end)))
disp('Lower step-size gives better results.')

function y = my_euler(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
y(i+1) = y(i) + h*f(t(i), y(i)); % Euler Update
end
end

Output:

Part (a)
----------------------------------
For h = 0.1, y(1) = 1.320016

Part (b)
----------------------------------
For h = 0.05, y(1) = 1.355880

Part (c)
----------------------------------
For h = 0.1, Error = 0.075597
For h = 0.05, Error = 0.039732
Lower step-size gives better results.

Add a comment
Know the answer?
Add Answer to:
1. Consider the following differential equation. ag = ty, y(0)=1. dt (a) Use Euler's Method with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT