iT邦幫忙

0

C語言 牛頓迭代法 求輸入區間內的解

不明 2021-06-26 17:36:481568 瀏覽
  • 分享至 

  • xImage

輸入說明:二次多項式f(x)=ax^2+bx+c,在第一列輸入a,b,c。第二列輸入區間[x1,x2]、誤差值E
輸出說明:輸出迭代的次數及得到的解(到小數點第三位)。若無解則輸出無解。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-06-27 10:06:50
#include <stdio.h>
#include <math.h>

int main()
{
    int a,b,c;
    float x1, x2, E;
    float x,f1,f2;
    static int count=0;
    
    a = 1;
    b = 4;
    c = 3;
    x1 = 1.5;
    x2 = 10.0;
    E = 1e-5;
    
    if (b*b - (4*a*c) >= 0) {
        do {
            x  = x1;
            f1 = a*x*x+b*x+c;
            f2 = a*x+b;
            x1 = x-f1/f2;
            count++;
        } while ((fabs(x1-x)<=E)&&(x1<=x2));
        printf("%d %10.3f\n",count, x1);
    } else {
        printf("無解\n");
    }
    return 0;
}

誨人不倦

自愚娛人
/images/emoticon/emoticon73.gif

我要發表回答

立即登入回答