Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of the assignment is to use C++ to write Huen's Method (Improved Euler) to calculate the differential equation dy/dx = x^2 + y^2

The purpose of the assignment is to use C++ to write Huen's Method (Improved Euler) to calculate the differential equation dy/dx = x^2 + y^2 where y(0) =1 with 5 iterations and a step of .1.

I have posted what the output table is supposed to look like, but in my program I keep getting my calculations off ever so slightly. Would anyone be able to figure out where in my code I am messing up? Or could anyone maybe post a better way about writing the program to work properly? thank you.

image text in transcribed

image text in transcribed

image text in transcribed

#include #include #include using namespace std;

// consider the differential equation // for a given x and y, return v double f(double x, double y) { double v = pow(x, 2) + pow(y, 2); return v; }

// predicts the next value for a given (x, y) // and step size h using Euler method double predict(double x, double y, double h) { // value of next y(predicted) is returned double y1p = y + h * f(x, y); return y1p; }

// corrects the predicted value // using Modified Euler method double correct(double x, double y, double x1, double y1, double h) { // (x, y) are of previous step // and x1 is the increased x for next step // and y1 is predicted y for next step double e = 0.00001; double y1c = y1;

do { y1 = y1c; y1c = y + 0.5 * h * (f(x, y) + f(x1, y1)); } while (fabs(y1c - y1) > e);

// every iteration is correcting the value // of y using average slope return y1c; }

void printFinalValues(double x, double xn, double y, double h) {

while (x

// at every iteration first the value // of for next step is first predicted // and then corrected. cout

int main()

{ // here x and y are the initial // given condition, so x=0 and y=0.5 double x = 0, y = 1;

// final value of x for which y is needed double xn = .4;

// step size double h = 0.1;

printFinalValues(x, xn, y, h);

system("pause"); return 0; }

Write a program for Euler's, Improved Euler's, and Runge-Kutta (Fourth Order) Method. The inputs are the DE, the initial condition, the step size and the number of iterations n. =r? + 12 y(0)=1 h=0.1 n=5 dy The output is a table of values for n, x(n) and y(n) starting at n=0. Heun's Method (Improved Euler) Heun's (Improved Euler's ) Method yn+1 = yn + hf (x,, yn) h = 0.1 n+ I n + =, +1[78/.) n+1 Inx(n) o 0.00 10.10 2 0.20 | 3 0.30 40.40 5 0.50 0.60 y(n) 1 1.111 1.251530674 1.436057423 1.688007332 2.048770723 dy/dx = x^2 + y^2 y(x) = 1 x = 0 Step size (h) = .1 Iterations (n) = 5 x(n) y(n) 0.00 WNPO 3 0.10 0.20 0.30 0.40 0.50 Press any key to continue... 1.112367935 1.255556687 1.445325955 1.708164739 2.093743007

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions