C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 12:08 PM
owlman76 owlman76 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 owlman76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 38 sec
Reputation Power: 0
Need help modifying my C program to calculate error in rk4 method!!

Hello everyone
I have constructed my program to perform runge-kutta 4th order method to solve a simple harmonic oscillator. I need to construct the program though to calculate the exact solution as well and calculate the global error.

Here is my program so far:

Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define N 2                                   
#define MIN 0.0
#define K 1            /* spring constant */
#define M 1            /* mass of block */


void runge4(double x, double y[], double step);
double f(double x, double y[], int i);

main()

{   
   double x, y[N], v0, x0, tmax, dt;
   int j, n;
   
   FILE *output;                             /* save data in pa2.dat */
   output = fopen("pa2.dat","w");

   printf("\nPlease Enter initial position and velocity X0 and V0: ");
   scanf("%le %le", &x0, &v0);

   printf("\nPlease Enter Tmax and N: ");
   scanf("%le %d", &tmax, &n);

   dt = tmax/(n-1);
   
   y[0] = x0;                                /* initial position  */
   y[1] = v0;                                /* initial velocity  */

   
   fprintf(output, "%f\t%f\n", x, y[0]);

   for(x = MIN; x <= tmax ; x += dt)
   {
      runge4(x, y, dt);
      fprintf(output, "%f\t%f\n", x, y[0]);   /* position vs. time */
   }
   printf("data stored in rk4.dat\n\n");
   fclose(output);
}



void runge4(double x, double y[], double step)

{

   double h=step/2.0,                        
          t1[N], t2[N], t3[N],               
          k1[N], k2[N], k3[N],k4[N];
         
   int i;
 
   for (i=0; i<N; i++) t1[i] = y[i]+0.5*(k1[i]=step*f(x, y, i));
   for (i=0; i<N; i++) t2[i] = y[i]+0.5*(k2[i]=step*f(x+h, t1, i));
   for (i=0; i<N; i++) t3[i] = y[i]+    (k3[i]=step*f(x+h, t2, i));
   for (i=0; i<N; i++) k4[i] =                 step*f(x + step, t3, i);
      
}


/* equations defined */

double  f(double x, double y[], int i)
{
   if (i == 0) return((2*y[0]*(1 - y[1]));               /* RHS of first equation */
   if (i == 1) return((-y[1]*(1 - y[0]));        /* RHS of second equation */
}


The exact solutions for the function are:

x(t) = x0cos(wt) + v0/w*sin(wt)
v(t) = -wx0*sin(wt) + v0*cos(wt)


I obviously know my program lacks true time dependence which is needed but I am pretty clueless on how to actually include the exact solutions and calculate the global error between the two. Any pointers would be appreciated.

Reply With Quote
  #2  
Old December 7th, 2012, 01:25 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
Please use [code][/code] tags when posting code.
If you're quick, you might still be able to edit your first post.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 16th, 2012, 05:00 AM
admiraln admiraln is offline
Still Learning
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Montreal, Canada
Posts: 55 admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 18 h 57 m 51 sec
Reputation Power: 38
It's been done.

This has probably been solve before. A goggle search turned up
http://people.sc.fsu.edu/~jburkardt/c_src/rk4/rk4.html

You may have to look at a few other samples to get exactly what you are looking.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help modifying my C program to calculate error in rk4 method!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap