Question

10.16: Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method...

10.16: Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method (use the form of second-order Runge-Kutta method, Eqs(10.65),(10.66)). For function name and arguments use [x,y]=odeMIDPOINT(ODE,a,b,h,yINI). The input argument ODE is a name for the function that calculates dy/dx. It is a dummy name for the function that is imported into odeMIDPOINT. The arguments a and b define the domain of the solution, h is step size; yINI is initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use the function odeMIDPOINT to solve the ODE in Problem 10.2. Write a MATLAB program in a script file that solves the ODE twice, once by using h=0.8 and once using h=0.1. The program should also plot exact solution (given in prob. 10.2 and two numerical solutions (all in same figure).10.2: first-order ODE: dy/dx=x-xy/2 from x=1 to x=3.4 with y(1)=1. Use h=0.8a) solve with Euler's explicit method.b) solve with modified Euler methodc) solve with classical fourth-order Runge-Kutta method. The analytical solution is y=2-e^((1-x^2)/4). In each part, calculate the error between the true solution and numerical solution at the points where the numerical solution is determined.

Homework Answers

Answer #1

MATLAB Script:

close all
clear
clc

ODE = @(x,y) x - x*y/2;
a = 1; b = 3.4; yINI = 1;

h = 0.8;
[x1, y1] = odeMIDPOINT(ODE, a, b, h, yINI);

h = 0.1;
[x2, y2] = odeMIDPOINT(ODE, a, b, h, yINI);

xx = a:0.01:b;
plot(xx, 2 - exp((1 - xx.^2)/4), 'linewidth', 2), hold on
plot(x1, y1, 'o-', x2, y2, 'o-'), xlabel('x'), ylabel('y')
legend('Exact Solution', 'h = 0.8', 'h = 0.1', 'location', 'northwest')

function [x, y] = odeMIDPOINT(ODE, a, b, h, yINI)
x = a:h:b;
y(1) = yINI;
for i = 1:length(x)-1
k1 = h*ODE(x(i), y(i));
k2 = h*ODE(x(i) + h/2, y(i) + k1/2);
y(i+1) = y(i) + k2;
end
end

Plot:

Know the answer?
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
Similar Questions
Given (dy/dx)=(3x^3+6xy^2-x)/(2y) with y=0.707 at x= 0, h=0.1 obtain a solution by the fourth order Runge-Kutta...
Given (dy/dx)=(3x^3+6xy^2-x)/(2y) with y=0.707 at x= 0, h=0.1 obtain a solution by the fourth order Runge-Kutta method for a range x=0 to 0.5
Solve the first-order differential equation by any appropriate method. (Enter your solution in the form F(x,...
Solve the first-order differential equation by any appropriate method. (Enter your solution in the form F(x, y) = C or y = F(x, C) where C is a needed constant.) y dx + (9x + 100y) dy = 0
Use C++ in Solving Ordinary Differential Equations using a Fourth-Order Runge-Kutta of Your Own Creation Assignment:...
Use C++ in Solving Ordinary Differential Equations using a Fourth-Order Runge-Kutta of Your Own Creation Assignment: Design and construct a computer program in C++ that will illustrate the use of a fourth-order explicit Runge-Kutta method of your own design. In other words, you will first have to solve the Runge-Kutta equations of condition for the coefficients of a fourth-order Runge-Kutta method.   See the Mathematica notebook on solving the equations for 4th order RK method.   That notebook can be found at...
The indicated function y1(x) is a solution of the given differential equation. Use reduction of order,...
The indicated function y1(x) is a solution of the given differential equation. Use reduction of order, to find a second solution dx **Please do not solve this via the formula--please use the REDUCTION METHOD ONLY. y2(x)= ?? Given: y'' + 2y' + y = 0;    y1 = xe−x
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits by following the instructions noted below. The input signal and impulse response generation should be done in the function. The test bench script should be used only to call the function and for signal plotting purposes. No plotting should be done inside the function itself. Name your function L2_C Instructions: Input voltage ,x(t), can be AC or DC. Consider a variable ‘w1’ which can...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT