十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
//多項式求導數(shù)
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站制作、成都網(wǎng)站制作與策劃設計,通化網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設10多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:通化等地區(qū)。通化做網(wǎng)站價格咨詢:18980820575
intPolyDeri(listnodePolypolyFunc)
{
listnodePoly::iteratoriter;
for(iter=polyFunc.begin();iter!=polyFunc.end();++iter)
{
if((*iter).ex1)
{
(*iter).coef=((*iter).coef)*((*iter).ex);
(*iter).ex=(*iter).ex-1;
}
elseif(1==(*iter).ex)
{
(*iter).ex=0;
}
elseif(0==(*iter).ex)
{
(*iter).coef=0;
}
}
returnRET_OK;
}
其中,多項式的定義是listnodePoly,如下:
//多項式節(jié)點結構體定義
typedefstructstuPolynomNode
{
doublecoef;
intex;
}nodePoly;
擴展資料
c語言求導數(shù)據(jù)范圍及提示DataSizeHint
#includeiostream
#includecmath
usingnamespacestd;
intmain()
{
intnum=0,i=0;
cinnum;
for(i=2;i=sqrt(num);i++)
{
if(num%i==0)
break;
}
if(isqrt(num)
coutnum"為素數(shù)"endl;
else
coutnum"不是素數(shù)"endl;
return0;
}
求導數(shù)有兩種,一種是表達式求導,一種是數(shù)值求導。
表達式求導:需要對表達式進行詞法分析,然后用常見的求導公式進行演算,求得導函數(shù)。在這方面,數(shù)學軟件matrix,maple做得非常好。如果自己用C進行編程,不建議。
數(shù)值求導:利用導數(shù)的定義,用差分計算,當自變量趨于0時,前后兩次差分收斂到需要精度,計算結束。這種方法可以求得某一點的導數(shù)。
例如:
求一階導數(shù),原函數(shù) y = f(x), 程序中是float f(float x){ ...}
dx=0.01;????//設?dx?初值
do{
dd1=(f(x0)?-?f(x0+dx))/dx;????//計算導數(shù)dd1
dx?=?0.5?*?dx;??//?減小步長
dd2=(f(x0)?-?f(x0+dx))/dx;????//計算導數(shù)dd2
}while?(fabs(dd1-dd2)?=?1e-06)?//判斷新舊導數(shù)值之差是否滿足精度,滿足則得結果,不滿足則返回
這位是在別人的地方copy來的,這是地址:
//一元多項式的求導
#includestdio.h
#includemalloc.h//動態(tài)申請空間的函數(shù)的頭文件
typedef struct node //定義節(jié)點類型
{
float coef; //多項式的系數(shù)
int expn; //多項式的指數(shù)
struct node * next; //結點指針域
}PLOYList;
void insert(PLOYList *head,PLOYList *input) //查找位置插入新鏈節(jié)的函數(shù),且讓輸入的多項式呈降序排列
{
PLOYList *pre,*now;
int signal=0;
pre=head;
if(pre-next==NULL) {pre-next=input;} //如果只有一個頭結點,則把新結點直接連在后面
else
{
now=pre-next;//如果不是只有一個頭結點,則設置now指針
while(signal==0)
{
if(input-expn now-expn)
{
if(now-next==NULL)
{
now-next=input;
signal=1;
}
else
{
pre=now;
now=pre-next;//始終讓新輸入的數(shù)的指數(shù)與最后一個結點中的數(shù)的指數(shù)比較,小于則插在其后面
}
}
else if( input-expn now-expn )
{
input-next=now;
pre-next=input;
signal=1;
}//若新結點中指數(shù)比最后一個結點即now中的指數(shù)大,則插入now之前
else//若指數(shù)相等則需合并為一個結點,若相加后指數(shù)為0則釋放該結點
{
now-coef=now-coef+input-coef;
signal=1;
free(input);
if(now-coef==0)
{
pre-next=now-next;
free(now);
}
}//else
} //while
}//else
}//void
PLOYList *creat(char ch) //輸入多項式
{
PLOYList *head,*input;
float x;
int y;
head=(PLOYList *)malloc(sizeof(PLOYList)); //創(chuàng)建鏈表頭
head-next=NULL;
scanf("%f %d",x,y);//實現(xiàn)用戶輸入的第一個項,包括其指數(shù)和系數(shù)
while(x!=0)//當用戶沒有輸入結束標志0時可一直輸入多項式的項,且輸入一個創(chuàng)建一個結點
{
input=(PLOYList *)malloc(sizeof(PLOYList)); //創(chuàng)建新鏈節(jié)
input-coef=x;
input-expn=y;
input-next=NULL;
insert(head,input); //每輸入一項就將其排序,是的鏈表中多項式呈降序排列
scanf("%f %d",x,y);
}
return head;
}
PLOYList *der(PLOYList *head)//多項式求導
{
PLOYList *p;
p = head - next;
while (p)
{
p - coef = p - coef * p - expn;
p - expn = p - expn--;
p = p - next;
}
return head;
}//將多項式的每項系數(shù)和指數(shù)相乘得到新的系數(shù),指數(shù)減一得到新的指數(shù)即完成求導
void print(PLOYList *fun) //輸出多項式,fun指要輸出的多項式鏈表的表頭
{
PLOYList *printing;
int flag=0;
printing=fun-next;
if(fun-next==NULL)//若為空表,則無需輸出
{
printf("0\n");
return;
}
while(flag==0)
{
if(printing-coef0fun-next!=printing)
printf("+");
if(printing-coef==1);
else if(printing-coef==-1)
printf("-");
else
printf("%f",printing-coef);
if(printing-expn!=0) printf("x^%d",printing-expn);
else if((printing-coef==1)||(printing-coef==-1))
printf("1");
if(printing-next==NULL)
flag=1;
else
printing=printing-next;
}
printf("\n");
}
void main()
{
PLOYList *f;
printf(" 注:輸入多項式格式為:系數(shù)1 指數(shù)1 系數(shù)2 指數(shù)2 …… ,并以0 0 結束:\n");
printf("請輸入一個一元多項式:");
f = creat('f');
printf("這個多項式為:f(x)= ");
print(f);
printf("求導結果為:F(x)=f'(x)= ");
f=der(f);
print(f);
printf("\n\n");
}
c語言求變量一階導數(shù)方法如下:
1、首先要有函數(shù),設置成double類型的參數(shù)和返回值。
2、然后根據(jù)導數(shù)的定義求出導數(shù),參數(shù)差值要達到精度極限,這是最關鍵的一步。
3、假如函數(shù)是doublefun(doubex),那么導數(shù)的輸出應該是(fun(x)-fun(x-e))/e,這里e是設置的無窮小的變量。
4、C由于精度有限,因此需要循環(huán)反復測試,并判斷無窮小e等于0之前,求出上述導數(shù)的值。二級導數(shù)也是一樣,所不同的是要把上述導數(shù)公式按定義再一次求導。這是算法,具體的實現(xiàn)自己嘗試編程。
一階導數(shù),微積分術語,一階導數(shù)表示的是函數(shù)的變化率,最直觀的表現(xiàn)就在于函數(shù)的單調(diào)性定理。
導數(shù)(英語:Derivative)是微積分學中重要的基礎概念。一個函數(shù)在某一點的導數(shù)描述了這個函數(shù)在這一點附近的變化率。導數(shù)的本質(zhì)是通過極限的概念對函數(shù)進行局部的線性逼近。當函數(shù)f的自變量在一點x0上產(chǎn)生一個增量h時,函數(shù)輸出值的增量與自變量增量h的比值在h趨于0時的極限如果存在,即為f在x0處的導數(shù)。
f1(x)=...
這是試圖為函數(shù)賦值?!這是不允許的
而且遞歸時沒有結束條件。
... ...
用差分計算,當自變量趨于0時,前后兩次差分收斂到需要精度,計算結束。
例如,一階導數(shù),寫一個函數(shù)y=f(x):
floatf(floatx){...}
設dx初值
計算dy
dy=f(x0)-f(x0+dx);
導數(shù)初值
dd1=dy/dx;
Lab:;
dx=0.5*dx;//減小步長
dy=f(x0)-f(x0+dx);
dd2=dy/dx;//導數(shù)新值
判斷新舊導數(shù)值之差是否滿足精度,滿足則得結果,不滿足則返回
if(fabs(dd1-dd2)1e-06){得結果dd2...}
else{dd1=dd2;gotoLab;};