當前位置:首頁 » 編程語言 » c語言里怎麼求導
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言里怎麼求導

發布時間: 2022-05-08 16:03:53

c語言求導

if(fabs(dd2-dd1)>=1e-6) {dd1=dd2;goto Lab;}
會不會死循環了

⑵ C語言求導問題

用差分計算,當自變數趨於0時,前後兩次差分收斂到需要精度,計算結束。
例如,一階導數,寫一個 函數 y = f(x):

float f(float x)

設 dx 初值
計算 dy
dy = f(x0) - f(x0+dx);
導數 初值
dd1=dy/dx;
Lab:;
dx = 0.5 * dx; // 減小步長
dy = f(x0) - f(x0+dx);
dd2=dy/dx; // 導數 新值
判斷新舊導數值之差是否滿足精度,滿足則得結果,不滿足則返回
if ( fabs(dd1-dd2) < 1e-06 )
else ;

⑶ c語言,求導

這位是在別人的地方來的,這是地址:
http://blog.csdn.net/qq_26816591/article/details/48690359
//一元多項式的求導
#include<stdio.h>
#include<malloc.h>//動態申請空間的函數的頭文件
typedef struct node //定義節點類型
{
float coef; //多項式的系數
int expn; //多項式的指數
struct node * next; //結點指針域
}PLOYList;
void insert(PLOYList *head,PLOYList *input) //查找位置插入新鏈節的函數,且讓輸入的多項式呈降序排列
{
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;//始終讓新輸入的數的指數與最後一個結點中的數的指數比較,小於則插在其後面
}
}
else if( input->expn > now->expn )
{
input->next=now;
pre->next=input;
signal=1;
}//若新結點中指數比最後一個結點即now中的指數大,則插入now之前
else//若指數相等則需合並為一個結點,若相加後指數為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)); //創建鏈表頭
head->next=NULL;
scanf("%f %d",&x,&y);//實現用戶輸入的第一個項,包括其指數和系數
while(x!=0)//當用戶沒有輸入結束標志0時可一直輸入多項式的項,且輸入一個創建一個結點
{
input=(PLOYList *)malloc(sizeof(PLOYList)); //創建新鏈節
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;
}//將多項式的每項系數和指數相乘得到新的系數,指數減一得到新的指數即完成求導

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->coef>0&&fun->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(" 註:輸入多項式格式為:系數1 指數1 系數2 指數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語言編寫的求導的程序

呵呵 求導函數 。。。。。。。。。。。。。。。。。。
C語言里有常用的函數比較簡單,對於單一項目簡單函數直接套用數學公式就可以了
而多項式函數就比較麻煩了 ,,還有復合函數本身求導過程就復雜。
如果你要實現任意形式的函數求導的話,必須完成正則表達函數提取出單項式,如有單項復合的情況下還要一一拆解,,直到能用數學公式求導的最基本函數才行!
還可以用同樣的方法完成定積分的計算。
實現王能推導函數的過程需要多個演算法的有機結合才行。。
就3步:
一:運用正則表達式演算法把函數的每一項抽取出來,對應簡單多項情況
二:運用正則表達式演算法把函數的每一項子項抽取出來,並建立父子關系,,對於復合函數
三:用數學公式求導

用程序最難實現的就是前兩相

⑸ 用C語言如何編寫函數的求導

求導數有兩種,一種是表達式求導,一種是數值求導。

  1. 表達式求導:需要對表達式進行詞法分析,然後用常見的求導公式進行演算,求得導函數。在這方面,數學軟體matrix,maple做得非常好。如果自己用C進行編程,不建議。

  2. 數值求導:利用導數的定義,用差分計算,當自變數趨於0時,前後兩次差分收斂到需要精度,計算結束。這種方法可以求得某一點的導數。

    例如:

    求一階導數,原函數 y = f(x), 程序中是float f(float x){ ...}

    dx=0.01;//設dx初值
    do{
    dd1=(f(x0)-f(x0+dx))/dx;//計算導數dd1
    dx=0.5*dx;//減小步長
    dd2=(f(x0)-f(x0+dx))/dx;//計算導數dd2
    }while(fabs(dd1-dd2)>=1e-06)//判斷新舊導數值之差是否滿足精度,滿足則得結果,不滿足則返回

⑹ c語言 多項式求導

#include<stdio.h> #include<stdlib.h> typedef struct polynode { int conf;/*常數*/ int exp;/*指數*/ struct polynode *next; }polynode; int main() { polynode *p,*q,*h; /*建立多項式*/ int conf,exp; h=(polynode*)malloc(sizeof(polynode)); q=h; scanf("%d %d",&conf,&exp); while(conf!=-1||exp!=-1) { p=(polynode*)malloc(sizeof(polynode)); p->conf=conf; p->exp=exp; q->next=p; q=p; scanf("%d %d",&conf,&exp); } q->next=NULL; \\你是這里寫錯了,寫成NUll了 q=h; while(q->next!=NULL) /*求導*/ { q=q->next; if(q->exp>=1) { q->conf=(q->conf)*(q->exp); q->exp-=1; } else if(q->exp==0) { q=NULL; } else { printf("error"); break; } } q=h; while(q->next!=NULL)/*輸出*/ { q=q->next; printf("%d %d",q->conf,q->exp); } return 0; } 改成這樣就能運行了

⑺ 用c語言如何求導

用差分計算,當自變數趨於0時,前後兩次差分收斂到需要精度,計算結束。
例如,一階導數,寫一個函數y=f(x):
floatf(floatx){...}
設dx初值
計算dy
dy=f(x0)-f(x0+dx);
導數初值
dd1=dy/dx;
Lab:;
dx=0.5*dx;//減小步長
dy=f(x0)-f(x0+dx);
dd2=dy/dx;//導數新值
判斷新舊導數值之差是否滿足精度,滿足則得結果,不滿足則返回
if(fabs(dd1-dd2)<1e-06){得結果dd2...}
else{dd1=dd2;gotoLab;};

⑻ C語言求解導數方程

M的倒數不就是 1/M 嗎。

Y=a*M'+b*M+c*N

就是Y=a/M + b/M + c/N

⑼ 怎樣用c語言實現對方程求導

求導數有兩種,一種是表達式求導,一種是數值求導。 表達式求導:需要對表達式進行詞法分析,然後用常見的求導公式進行演算,求得導函數。在這方面,數學軟體matrix,maple做得非常好。如果自己用C進行編程,不建議。 數值求導:利用導數的定義

⑽ c語言怎麼編求導

//多項式求導數

intPolyDeri(list<nodePoly>&polyFunc)

{

list<nodePoly>::iteratoriter;

for(iter=polyFunc.begin();iter!=polyFunc.end();++iter)

{

if((*iter).ex>1)

{

(*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;

}

其中,多項式的定義是list<nodePoly>,如下:

//多項式節點結構體定義

typedefstructstuPolynomNode

{

doublecoef;

intex;

}nodePoly;

(10)c語言里怎麼求導擴展閱讀

c語言求導數據范圍及提示DataSize&Hint

#include<iostream>

#include<cmath>

usingnamespacestd;

intmain()

{

intnum=0,i=0;

cin>>num;

for(i=2;i<=sqrt(num);i++)

{

if(num%i==0)

break;

}

if(i>sqrt(num)

cout<<num<<"為素數"<<endl;

else

cout<<num<<"不是素數"endl;

return0;

}