can someone check this

This is a discussion on can someone check this within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that executes the improved Euler method of curve fitting. I am pretty sure ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    Question can someone check this

    I am trying to write a program that executes the improved Euler method of curve fitting. I am pretty sure i have written the code correctly to run it, but when it runs, the numbers get massive very quickly, and i worry i;m making a fundamental (C programming) error.

    thanks

    Code:
    int Euler(t, u, v, x0, y0, z0)
    
        float t, u, v, x0, y0, z0;
        {
            float x[100], y[100], z[100], dxdt_i, dydt_i, dzdt_i,dx, dy, dz, dxdt_new, dydt_new, dzdt_new, dt = 1;
            int i;
    
            x[0] = x0;
            y[0] = y0;
            z[0] = z0;
    
            system("cls");
            printf ("\nLorenz formulas:\n\n");
            printf("dx                          dy                             dz \n");
            printf("--  = a(y - x)       :      --  = x(b - z) - y      :      --  = xy - cz\n");
            printf("dt                          dt                             dt\n\n\n");
    
    
            for (i = 0; i<5; i++)
            {
    
            dxdt_i = t*(y[i] - x[i]);
            dydt_i = x[i]*(u - z[i]) - y[i];
            dzdt_i = (x[i]*y[i]) - (v*z[i]);
    
            dx = (t*(y[i] - x[i]))*dt;
            dy = (x[i]*(u - z[i]) - y[i])*dt;
            dz = ((x[i]*y[i]) - (v*z[i]))*dt;
    
    
            dxdt_new = t*(y[i] - (x[i] + dx));
            dydt_new = x[i]*(u - z[i]) - (y[i] + dy);
            dzdt_new = (x[i]*y[i]) - (v*(z[i] + dz));
    
            x[i+1] = x[i] +(dt/2)*(dxdt_i + dxdt_new);
            y[i+1] = y[i] +(dt/2)*(dydt_i + dydt_new);
            z[i+1] = z[i] +(dt/2)*(dzdt_i + dzdt_new);
    
            printf("dx                          dy                             dz\n");
            printf("--  = %.2f          :      --  = %.2f             :      --   = %.2f\n", dxdt_i, dydt_i, dzdt_i);
            printf("dt                          dt                             dt\n");
            printf("\nnew xgrad = %.3f new ygrad = %.3f new zgrad = %.3f\n\n", dxdt_new, dydt_new, dzdt_new);
            printf("\nnew x = %.3f new y = %.3f new z = %.3f\n\n", x[i+1], y[i+1], z[i+1]);
    
           i =  i++;
        }
        }
    P.S at the top where i sent the function the variables for x0, y0 and z0 and then on the next line i do this:

    x[0] = x0;
    y[0] = y0;
    z[0] = z0;

    its because i keep getting an error if i try and send the function a varaible to go straight into x[0]. I.e

    int Euler(t, u, v, x[0], y[0], z[0])

    can you not do this, it would neated things up abit...

    also how do you control significant figures, on the off chance that it is working correctly, the numbers are too big and ruin my display.

    If you cant control sigfigs, when i write this bit:

    Code:
    printf("dx                          dy                             dz\n");
            printf("--  = %.2f          :      --  = %.2f             :      --   = %.2f\n", dxdt_i, dydt_i, dzdt_i);
            printf("dt                          dt                             dt\n");
    if the floating variables are big, then it messes up my nice rows. Can i control it so it is nicely tabulated?
    Last edited by a.mlw.walker; 03-28-2009 at 06:45 PM. Reason: forgot somehting...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course you can't pass in x[0], since the array x doesn't exist yet. Passing in a variable as you have it is the proper way to go.

    If you haven't looked up printf yet, do so -- the f stands for formatted, after all, so you can specify your output format pretty exactly.

    I didn't trace out the math very exactly, but I am not aware of a calculus-based formula for which setting a differential element (like dt) equal to 1 is even slightly appropriate. If 0.01 works, you're lucky, 0.001 or smaller is not uncommon.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    If the x axis of a graph is time, the accuracy of my curve fit depends on how many points along the x axis i choose. For this i said that i would plot a point for every second that passes. Hence dt = 1.

    Thank you for telling me about printf, when you begin programming, all you hear is printf lets you print to screen - i'll look it up now.

    I just thought that if i could say

    int lorenz (a, b)
    float a, b;

    i thought i might be able to say

    int lorenz (a[0], b)
    float a[10], b;

    but if i have done it correctly thats fine, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21