作者kikiqqp (貓食罐頭)
看板C_and_CPP
標題Re: [問題] 請益二分法與牛頓法...
時間Wed Oct 28 19:00:15 2009
以下Code在Dev-C++執行正常 使用C語言模式
我不確定有沒有寫錯(爆) 手邊也有沒東西讓我驗證
因為原PO的牛頓法我覺得有點怪
反正題目變了就a1 b1 fx1(二分) f0 f1(牛頓)這些改一改就可以算了
code醜就算了 一邊看Saki一邊寫的
~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~
//二分法
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_number 200000
int main(int argc, char *argv[])
{
double a, b, x1, a1, fx1, b1, eps, add, as;
int i = 1;
printf("-= Method of bisection =-\n");
printf("230x^4 + 18x^3 + 9x^2 - 211x - 9 = 0
, Please input a, b and eps. f(a) < 0.
\n---------------------------------------------\n");
printf("Input a (-1): ");
scanf("%lf", &a);
printf("Input b (0): ");
scanf("%lf", &b);
printf("Input eps (0.000001): ");
scanf("%lf", &eps);
printf("NO: a x b f
(a) f(x1) f(b)\n");
do
{
add = 0.0;
x1=(a + b) / 2.0;
a1=pow((230.0 * a), 4.0) + pow((18.0 * a), 3.0) +
pow((9.0 * a), 2.0) - 211.0 * a - 9.0;
fx1=pow((230.0 * x1), 4.0) + pow((18.0 * x1), 3.0) +
pow((9.0 * x1), 2.0) - 211.0 * x1 - 9.0;
b1=pow((230.0 * b), 4.0) + pow((18.0 * b), 3.0) +
pow((9.0 * b), 2.0) - 211.0 * b - 9.0;
if(i >= MAX_number || i == a || x1 == b || a ==b || a1*b1 > 0)
{
system("cls");
printf("Error input or out of compass....\n");
i = 0;
break;
}
if(i < 10)
printf("%d %f %f %f %.4e %01.4e %01.4e\n",
i ,a ,x1 ,b ,a1 ,fx1 ,b1);
else
printf("%d %f %f %f %.4e %01.4e %01.4e\n",
i ,a ,x1 ,b ,a1 ,fx1 ,b1);
if(a1 * fx1 < 0.0)
{
a = a;
b = x1;
}
else if(fx1 * b1 < 0.0)
{
a = x1;
b = b;
}
as = x1;
x1 = 0.0;
add = fabs(fx1);
i++;
}
while(add >= eps);
if(i > 0)
{
printf("\nf(x1)= %e\n", fx1);
printf("x1= %f\n", as);
}
system("PAUSE");
return 0;
}
~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~~( ̄▽ ̄)~(_△_)~( ̄▽ ̄)~
牛頓法
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_number 1000
int main(int argc, char *argv[])
{
double x_0, x_1, x_2, f0, f1;
int i = 1;
printf("-= Newton's method =-\n");
printf("230x^4 + 18x^3 + 9x^2 - 211x - 9 = 0
, Please input a, b and eps. x0 > 0.
\n---------------------------------------------\n");
printf("Input x0: ");
scanf("%lf", &x_0);
do
{
f0=pow((230.0*x_0), 4.0)+ pow((18.0*x_0), 3.0)
+ pow((9.0*x_0), 2.0) - 211.0 * x_0 - 9.0;
f1=pow(920.0, x_0) + pow(54.0, x_0) - 211.0;
//((920*p0*p0*p0)+(54*p0*p0)-211 <--- realy?
x_1= x_0 - ( f0 / f1 );
if(i >= MAX_number)
{
system("cls");
printf("Error input or out of compass....\n");
i = 0;
break;
}
if(i < 10)
printf("x%d %f\n", i, x_1);
else
printf("x%d %f\n", i, x_1);
if((float)x_0 == (float)x_1)
break;
i++;
x_0=x_1;
}
while(1);
if(i > 0)
printf("\nAt x= %f \n", x_1);
system("PAUSE");
return 0;
}
※ 引述《moon2519 (~輝~)》之銘言:
: Dev-C++
: ------------二分法-------------------------
: float term;
: float a=-1 ,b=0 ,t=1/100000 ,N;
: float p , q ,temp ,FA ,FP;
: cin >> N;
: term = (230*a*a*a*a) + (18*a*a*a) + (9*a*a) - (211*a) -9;
: FA=term;
: for(int i=1; i <= N ; i++){
: p = a+((b-a)/2);
: FP=(230*p*p*p*p) + (18*p*p*p) + (9*p*p) - (211*p) -9;
: cout<<"term :"<<FA<<endl;
: cout<<"temp :"<<FP<<endl;
: cout<<"temp*term :"<<FP*FA<<endl;
: if( FP==0||((b-a)/2)<t){
: cout<<"p = "<<p<<endl;
: cout<<"FP = "<<FP<<endl;
: break;
: }
: if((FP*FA)>0){
: a=p;
: FA=FP;
: }
: if((FP*FA)<0){
: b=p;
: }
: }
: ------------牛頓法-------------------------
: float t=1/1000000,p0=0,p=-1,N0,sign;
: cin>>N0;
: for(int i=1; i<=N0; i++){
: p=p0-((230*p0*p0*p0*p0)+(18*p0*p0*p0)+(9*p0*p0)-(211*p0)-9)/((920*p0*p0*p0)+(54*p0*p0)-211);
: sign=(p-p0);
: if(sign<=0){
: sign=(-1)*sign;
: }
: if(sign<=t){
: cout<<p<<endl;
: }
: p0=p;
: }
--
容我血尿一下 我用的telnet軟體不是PCMAN
底置的程式碼上色因為不能選擇自訂按鍵格式(自訂成^C\x7F\x7F)
所以我不能直接複製貼上呀 orz
只好慢慢改
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.119.110
推 VictorTom:還真沒注意到那個網頁其他telnet軟體可能不行....XD 10/28 19:09
→ nowar100:血尿 XD 10/28 19:54
→ MOONRAKER:你f(x)搞錯了吧 pow(230*x, 4) 是 (230x)^4 10/28 20:09
→ MOONRAKER:會變成2798140000x^4,這樣真的好嗎? 10/28 20:10
推 VictorTom:M大好眼力....XD 10/28 20:10
→ MOONRAKER:因為我前晚無聊寫了一下 覺得實在不用寫成這樣的 10/28 20:11
→ MOONRAKER:程式在家裡,晚點看要不要貼上來,其實也滿傷眼的 10/28 20:12
→ kikiqqp:阿啦阿啦阿啦~~~( ̄y▽ ̄)╭ 真的寫錯式子了耶 晚點再來改 10/28 20:34
推 aecho:囧rz pietty也不行直接貼~~ (飄走 10/28 22:12