Question

Matlab code needed for: 1. Apply the Explicit Trapezoid Method on a grid of step size h = 0.1 in [0, 1] to the initial value

Please do not use SYMS package. It does not work on Octave for me.

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

Here is the matlab code please do upvote thankyou.

MATLAB Code:

close all
clear
clc

syms Y(T)
h = 0.1; ti = 0; tf = 1; n = (tf - ti)/h; t = ti:h:tf;
y(1) = 1;

fprintf('Problem (a)\n---------------------------------------------------\n')
func = @(t,y) t;
func_exact = dsolve(diff(Y,T) == T, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

fprintf('\nProblem (b)\n---------------------------------------------------\n')
func = @(t,y) t^2 * y;
func_exact = dsolve(diff(Y,T) == T^2 * Y, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

fprintf('\nProblem (c)\n---------------------------------------------------\n')
func = @(t,y) 2*(t+1)*y;
func_exact = dsolve(diff(Y,T) == 2*(T+1)*Y, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

fprintf('\nProblem (d)\n---------------------------------------------------\n')
func = @(t,y) 5 * t^4 * y;
func_exact = dsolve(diff(Y,T) == 5 * T^4 * Y, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

fprintf('\nProblem (e)\n---------------------------------------------------\n')
func = @(t,y) 1/y^2;
func_exact = dsolve(diff(Y,T) == 1/Y^2, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

fprintf('\nProblem (f)\n---------------------------------------------------\n')
func = @(t,y) t^3 / y^2;
func_exact = dsolve(diff(Y,T) == T^3 / Y^2, Y(0) == 1);
for i=1:n
k1 = func(t(i), y(i));
k2 = func(t(i) + h, y(i) + h*k1);
y(i + 1) = y(i) + 0.5*h*(k1 + k2);
end
fprintf('%-8s\t\t%-20s%-20s\n', 'ti', 'wi', 'error')
for i = 1:length(t)
fprintf('%-.8f\t\t%-20.12f%-20.12f\n', t(i), y(i), abs(y(i) - subs(func_exact, t(i))))
end

---OUTPUT--

Problem (a) ti 0.00000000 0.10000000 0.20000000 0.30000000 0.40000000 0.50000000 0.60000000 0.70000000 0.80000000 0.90000000

Problem (d) ti 0.00000000 0.10000000 0.20000000 0.30000000 0.40000000 0.50000000 0.60000000 0.70000000 0.80000000 0.90000000

Add a comment
Know the answer?
Add Answer to:
Please do not use SYMS package. It does not work on Octave for me. Matlab code...
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
  • MATLAB Code. Try not to use SYMS package as it does not load on Octave. Use...

    MATLAB Code. Try not to use SYMS package as it does not load on Octave. Use the Backward Difference Method to solve the problems of Computer Problem 1. Make a table of the exact value, the approximate value, and error at (x, t) = (0.5,1) for step sizes h = 0.02 and k = 0.02,0.01,0.005. REFERENCE: CP1: 1. Solve the equation u; = 2uxx for 0 < x <1,0<< 1, with the initial and boundary conditions that follow, using the...

  • Adams Fourth-Order Predictor-Corrector Python ONLY!! Please translate this pseudocode into Python code, thanks!! Adams Fourth-Order Predictor-Corrector...

    Adams Fourth-Order Predictor-Corrector Python ONLY!! Please translate this pseudocode into Python code, thanks!! Adams Fourth-Order Predictor-Corrector To approximate the solution of the initial-value problem y' = f(t, y), ast<b, y(a) = a, at (N + 1) equally spaced numbers in the interval [a, b]: INPUT endpoints a, b; integer N; initial condition a. OUTPUT approximation w to y at the (N + 1) values of t. Step 1 Set h = (b − a)/N; to = a; Wo = a;...

  • Please help me do both problems if you can, this is due tonight and this is...

    Please help me do both problems if you can, this is due tonight and this is my last question for this subscription period. (Thank you) Euler's method for a first order IVP y = f(x,y), y(x) = yo is the the following algorithm. From (20, yo) we define a sequence of approximations to the solution of the differential equation so that at the nth stage, we have In = {n-1 +h, Yn = Yn-1 +h. f(xn-1, Yn-1). In this exercise...

  • Please show all steps and equations used, please write neatly, thank you! please answer all parts...

    please show all steps and equations used, please write neatly, thank you! please answer all parts and explain the process. Problem 17. Consider the following multistep methods for the initial value problem y f(t, v), y(to) = α on the interval (to, to + d). Below t.-to+ih, h-d/N, and wi is an approximation to y(ti): ()j41 3131+3 4h Milne's method (3) We assume that the needed intial values wo, ..., wm for an m-step methods are generated by an one-step...

  • SOLVE USING MATLAB ONLY AND SHOW FULL CODE. PLEASE TO SHOW TEXT BOOK SOLUTION. SOLVE PART D ONLY

    SOLVE USING MATLAB ONLY AND SHOW FULL CODE. PLEASE TO SHOW TEXT BOOK SOLUTION. SOLVE PART D ONLY Apply Euler's Method with step sizes h # 0.1 and h 0.01 to the initial value problems in Exercise 1. Plot the approximate solutions and the correct solution on [O, 1], and find the global truncation error at t-1. Is the reduction in error for h -0.01 consistent with the order of Euler's Method? REFERENCE: Apply the Euler's Method with step size...

  • Exercise 3 is used towards the question. Please in MATLAB coding. 1. Apply Euler's Method with...

    Exercise 3 is used towards the question. Please in MATLAB coding. 1. Apply Euler's Method with step size h=0.1 on [0, 1] to the initial value problems in Exercise 3. Print a table of the t values, Euler approximations, and error (difference from exact solution) at each step. 3. Use separation of variables to find solutions of the IVP given by y) = 1 and the following differential equations: (a) y'=1 (b) y'=1y y'=2(1+1)y () y = 5e4y (e) y=1/92...

  • this is numerical analysis. Please do a and b 4. Consider the ordinary differential equation 1'(x)...

    this is numerical analysis. Please do a and b 4. Consider the ordinary differential equation 1'(x) = f(x, y(x)), y(ro) = Yo. (1) (a) Use numerical integration to derive the trapezoidal method for the above with uniform step size h. (You don't have to give the truncation error.) (b) Given below is a multistep method for solving (1) (with uniform step size h): bo +1 = 34 – 2n=1 + h (362. Yn) = f(n=1, 4n-1)) What is the truncation...

  • Is it possible to do this without matlab? 3In modelling the velocity y of a chain slipping off a horizontal platform, th...

    Is it possible to do this without matlab? 3In modelling the velocity y of a chain slipping off a horizontal platform, the differential equation y'- 10/y - y/x is derived. Suppose the initial condition is y (1)1 (a) Euler's method for solving y-f(x,y), y(XO-yo, is given byYn+1-yn+hf(xn,Yn) where h is a fixed stepsize, xnxo nh, and yn ~y(x). Apply one step of Euler's method to the initial value problem given above (b) Apply one step of the improved Euler method...

  • The outputs are very close to the correct values so I assume I just typed an...

    The outputs are very close to the correct values so I assume I just typed an equation incorrectly, but I have not been able to find the error. Courses LMS integration Documentation Write a function to implement the 4th order Runge Kutta method function [ty] - r4(f, range, ich) where is an anonymous function that defines the differential equation range is a vector of length 2 that sets a time range for a range for the independent art the initial...

  • I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear...

    I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear all; format long; f=@(t,y)y*(1-y); y(1)=0.01; %%%% Exact solution [t1 y1]=ode45(f,[0 9],y(1)); figure; plot(t1,y1,'*'); hold on % Eular therom M=[32 64 128]; T=9; fprintf(' M Max error \n' ); for n=1:length(M) k=T/M(n); t=0:k:T; for h=1:length(t)-1 y(h+1)=y(h)+k*f(t(h),y(h)); end plot(t,y); hold on %%%...

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