s12 - Ma 449: Numerical Applied Mathematics Model Solutions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Ma 449: Numerical Applied Mathematics Model Solutions to Homework Assignment 12 Prof. Wickerhauser Algorithm 13 of Section 9.7, p.526. C PROGRAM: /* Predator-prey model, a system of 2 nonlinear IVPs, solved with RK4 */ #include <stdio.h> #define N 50 const double A=2, B=0.02, C=0.0002, D=0.8; typedef struct { double x; double y; } Vector; double f(double t, double x, double y){ return A*x-B*x*y; } double g(double t, double x, double y){ return C*x*y-D*y; } Vector rk4step(double t, Vector in, double h) { double x, y, f1, f2, f3, f4, g1, g2, g3, g4, hh; Vector out; hh=h/2; x=in.x; y=in.y; f1=f(t, x, y); g1=g(t, x, y); f2=f(t+hh,x+hh*f1,y+hh*g1); g2=g(t+hh,x+hh*f1,y+hh*g1); f3=f(t+hh,x+hh*f2,y+hh*g2); g3=g(t+hh,x+hh*f2,y+hh*g2); f4=f(t+h, x+h*f3, y+h*g3); g4=g(t+h, x+h*f3, y+h*g3); out.x = h*(f1 + 2*f2 + 2*f3 + f4)/6; out.y = h*(g1 + 2*g2 + 2*g3 + g4)/6; return out; } void solve( Vector w[], double h ){ /* w[0] must be defined */ int k; Vector dw; for(k=0; k<N; k++) { dw = rk4step( k*h, w[k], h ); w[k+1].x = w[k].x + dw.x; w[k+1].y = w[k].y + dw.y; } return; } main(void) { int k; Vector x[N+1]; FILE *foxes, *rabbits; const double h=5.0/N; printf("H12, Sec 9.7, Alg 13, %d steps, h=%g\n", N, h); foxes = fopen("foxes.asc", "w"); rabbits = fopen("rabbits.asc", "w"); printf("Initial rabbits x(0): "); scanf("%lf", &(x[0].x)); printf("Initial foxes y(0): "); scanf("%lf", &(x[0].y)); solve( x, h ); for(k=0; k<=N; k++) { fprintf(rabbits, "%g %g\n", k*h, x[k].x); fprintf(foxes, "%g %g\n", k*h, x[k].y); } printf("Final rabbits: %.0fg; Final foxes: %.0f\n", x[N].x, x[N].y); fclose(rabbits); fclose(foxes); return 0; } OUTPUT: % ./a.out H12, Sec 9.7, Alg 13, 50 steps, h=0.1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initial rabbits x(0): 3000 Initial foxes y(0): 120 Final rabbits: 3018; Final foxes: 120 % ./a.out H12, Sec 9.7, Alg 13, 50 steps, h=0.1 Initial rabbits x(0): 5000 Initial foxes y(0): 100 Final rabbits: 5000; Final foxes: 100 See also the graphs of the solutions: part (a) part (b) -------------- -------------- foxes : ./9-7a13af.jpg ./9-7a13bf.jpg rabbits : ./9-7a13ar.jpg ./9-7a13br.jpg Problem H12.1. Write a computer program to solve the following initial value problem on the interval [0,1]: y'(t) = 2*t*y(t), 0<t<1; y(0)=1, using the fourth-order Runge-Kutta method. Have the program prompt the user for the number of steps N, then use h=1/N as the step size. Make a table of the values y(1) for N=10, N=100, and N=1000. Compare the results with the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/16/2010 for the course MATH 449 taught by Professor Wickerhauser during the Fall '09 term at Washington University in St. Louis.

Page1 / 6

s12 - Ma 449: Numerical Applied Mathematics Model Solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online