Метод Ньтона

Тема в разделе "C и С++ FAQ", создана пользователем vamp, 11 дек 2011.

  1. vamp

    vamp Гость

    #include <stdio.h>
    #include <graphics.h>
    #include <conio.h>
    #include <math.h>
    #include <process.h>
    #define PathToDriver "C:\\bc_\\"
    struct DataType
    {
    float x, y;
    };
    DataType* Data;
    int RecNum=0;
    float f(int func, float x)
    {
    switch (func)
    {
    case 1:
    return 0.348*x*x+0.696*x-8.348;
    break;
    case 2:
    return 8*sin(x/2+3.14/8);
    break;
    case 3:
    return 0.21*x*x*x+0.85*x*x-2.34*x-0.4;
    break;
    }

    return 0;
    }
    float f1(int func, float x)
    {
    switch (func)
    {
    case 1:
    return 0.696*x+0.696;
    break;
    case 2:
    return 4*cos(x/2+3.14/8);
    break;
    case 3:
    return 0.63*x*x+1.7*x-2.34;
    break;
    }
    return 0;
    }
    void DrawLogo()
    {
    gotoxy(20, 8);
    printf("------");
    gotoxy(20, 9);
    printf("! !");
    gotoxy(20, 10);
    printf("! This program is created !");
    gotoxy(20, 11);
    printf("! for demonstrating the Newton's method !");
    gotoxy(20, 12);
    printf("! of solving equations !");
    gotoxy(20, 13);
    printf("! !");
    gotoxy(20, 14);
    printf("! made by Ahramovich T.V. !");
    gotoxy(20, 15);
    printf("! !");
    gotoxy(20, 16);
    printf("---------");
    }
    void DrawMainMenu()
    {
    printf("! Exit !");
    printf(" File !");
    printf(" Solve !\n");
    printf("----------\n");
    }
    void Save()
    {
    FILE *stream;
    stream=fopen("Result.txt", "w+");
    fprintf(stream, "This file contains the results\n\nIteration x y\n");
    fprintf(stream, "-----\n");
    for (int i=0; i<RecNum-1; i++)
    fprintf(stream, " %d %f %f\n", i+1, Data.x, Data.y);
    fclose(stream);
    clrscr();
    DrawMainMenu();
    gotoxy(20, 9);
    printf("-------");
    gotoxy(20, 10);
    printf("! File Result.txt is succesfully saved !");
    gotoxy(20, 11);
    printf("! !");
    gotoxy(20, 12);
    printf("! Press any key to EXIT !");
    gotoxy(20, 13);
    printf("-----------");
    getch();
    }
    void Open()
    {
    FILE *stream;
    char *str;
    str=new char[255];
    clrscr();
    stream=fopen("Result.txt", "o");
    fscanf(stream, str);
    printf(str);
    delete str;
    fclose(stream);
    printf("\n\n-------");
    printf("\nPress any key to EXIT");
    getch();
    }

    void FileMenu()
    {
    char ch;
    clrscr();
    DrawMainMenu();
    printf(" ! Save !\n");
    printf(" ! Open !\n");
    printf(" ----------\n");
    ch=getch();
    switch (ch)
    {
    case 's':
    Save();
    break;
    case 'o':
    Open();
    break;
    default:
    break;
    }
    }
    void grafik(int func)
    {
    int gdriver=DETECT
    int g_mode;
    int g_error;
    initgraph(&g_driver,&g_mode,PathToD river);
    g_error=graphresult() ;
    if (g_error!=grOk)
    {
    printf("Error have been rised while initiatin graphiks \n",g_error);
    printf("error message is",g_error,grapherrormsg(g_error)) ;
    puts("to quit press <Enter>");
    getch();
    exit(1);
    }
    DrawMainMenu();
    setcolor(DARKGRAY);
    line(240, 80, 639, 80);
    line(240, 80, 240, 479);
    line(240, 479, 639, 479);
    line(639, 479, 639, 80);
    setcolor(WHITE);
    line(440, 90, 440, 470);
    line(250, 280, 630, 280);
    for (int i=0; i<18; i++)
    {
    line(i*20+260, 283, i*20+260, 277);
    line(437, 460-i*20, 443, 460-i*20);
    }
    line (630, 280, 620, 283);
    line (630, 280, 620, 277);
    line (440, 90, 443, 100);
    line (440, 90, 437, 100);
    outtextxy(620, 290, "x");
    outtextxy(420, 90, "y");

    float x=-8.3;
    float y;
    int px;
    int py;
    while (x<6.3)
    {
    y=f(func, x);
    px=20*x+440;
    py=-20*y+280;
    if (py>85)
    putpixel(px, py, YELLOW);
    x=x+0.001;
    }
    }

    void Newton(int func, double a)
    {
    RecNum=0;

    double x0, y0;
    int x, y;
    int i=0;
    x0=a;

    grafik(func);

    Data.x=x0;
    Data.y=f(func, x0);
    i++;
    printf("%f ", x0);
    printf("%f \n", f(func, x0));

    x=x0*20+440;
    y=-f(func, x0)*20+280;
    setcolor(RED);
    circle(x, y, 2);
    circle(x, y, 1);

    while (fabs(f(func, x0))>0.000001)
    {
    x0=x0-f(func, x0)/f1(func, x0);
    Data.x=x0;
    Data.y=f(func, x0);
    i++;
    printf("%f ", x0);
    printf("%f \n", f(func, x0));
    x=x0*20+440;
    y=-f(func, x0)*20+280;
    circle(x, y, 2);
    circle(x, y, 1);
    }

    RecNum=i;

    setcolor(GREEN);
    circle(x, y, 2);
    circle(x, y, 1);

    printf("\n-------");
    printf("\nPress any key to EXIT");
    }


    void SolveMenu()
    {
    char ch;

    clrscr();
    DrawMainMenu();
    printf(" ! 1: ax^2+bx+c [1, 8] !\n");
    printf(" ! 2: ax^3+bx^2+cx+d [-7, 1] !\n");
    printf(" ! 3: sin(x/k-w) [-2, 5] !\n");
    printf(" -------------\n");

    ch=getch();

    switch (ch)
    {
    case '1':
    clrscr();
    DrawMainMenu();
    Newton(1, 1);
    getch();
    closegraph();
    break;

    case '2':
    clrscr();
    DrawMainMenu();
    Newton(3, -7);
    getch();
    closegraph();
    break;

    case '3':
    clrscr();
    DrawMainMenu();
    Newton(2, -2);
    getch();
    closegraph();
    break;

    default:
    break;
    }
    }

    void MainMenu()
    {
    char ch;
    clrscr();
    DrawMainMenu();
    DrawLogo();

    ch=getch();

    while (ch!='e')
    {
    switch (ch)
    {
    case ('f'):
    FileMenu();
    break;

    case 's':
    SolveMenu();
    break;

    default:
    clrscr();
    break;
    }
    clrscr();
    DrawMainMenu();
    ch=getch();
    }
    }
    void main()
    {
    Data=new DataType [20];
    MainMenu();
    delete Data;
    }
     
  2. lazybiz

    lazybiz Well-Known Member
    C\C++ Team

    Регистрация:
    3 ноя 2010
    Сообщения:
    1.344
    Симпатии:
    0
    Ну давай, начинай. Мне тоже интересно где там ошибки и как их нужно исправлять.
     
  3. AlixBZ

    AlixBZ Гость

Загрузка...

Поделиться этой страницей