| 廣告聯系 | 簡體版 | 手機版 | 微信 | 微博 | 搜索:
歡迎您 游客 | 登錄 | 免費注冊 | 忘記了密碼 | 社交賬號注冊或登錄

首頁

溫哥華資訊

溫哥華地產

溫哥華教育

溫哥華財稅

新移民/招聘

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: XY | 亂想 | 五木森林 | 細雨飄渺 | 靜觀雲卷雲舒 | 羅蓬特機器人 | 大溫房產和地產研究 | 花隨風 | 豬頭看世界 | 顧曉軍 | Invisible world | rxmei | dwx | lxls | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 呱呱叫廚房 | 異鄉的世界 | 呂洪來的個人空間 | Notme | 逸言堂
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿PR卡未入籍的永久居民會被強制服兵役嗎?   這個銀條   如何修改會員名?
 論壇轉跳:
     發帖回帖獲取加西鎊, 兌換精彩禮物

論壇首頁 -> IT人生

埓蠹襩表看法和建h (發表於15年前)

分頁: 1, 2, 3, 4, 5, 6, 7  下一頁  



回復主題  圖片幻燈展示  增添帖子到書簽中  給帖子中的發貼者批量贈送獻花或者花籃    |##| -> |=|        發表新主題
閱讀上一個主題 :: 閱讀下一個主題  
作者 正文
router
(只看此人)




文章 時間: 2010-9-17 21:53 引用回復
被要求寫個C++程序來解決下面@個數學問},時間很緊,有點頭痛,想請大家幫幫忙,討論下,看能否理出一些兒頭緒:在此,我先謝謝各位了。

==============================

A root-finding algorithm is a numerical method, or algorithm, for finding a value x such that f(x) = 0, for a given function f. Such an x is called a root of the function f.

An example of a root finding method is the Newton-Raphson method. Essentially, to find the root, one repeatedly evaluates (where k is the kth iteration):
xk+1 = xk - f(xk)/f'(xk);
Where
o f'(x) is the first derivative of f with respect to x at the value x
o x0 = some_initial_x
Until f(xk) is 0.

Your task is to write a function in C++ specifically for the Newton-Raphson algorithm, keeping in mind that we may want to find a root of the following functions (where x is in radians):
Function Derivative
sin(x) cos(x)
cos(x) -sin(x)
sin(x) + 1 cos(x)
sin(x) + 2 cos(x)

Write or describe tests that test the boundaries of this algorithm.

====================================
 
(18)
花籃 (1)
分享
_________________
沒有希望的人生是無為的人生;充滿希望的人生是無畏的人生
樓主 | 電梯直達
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
router
(只看此人)




文章 時間: 2010-9-18 01:17 引用回復
我想, x 變量的初始值可以選在 1 到 89 之間, 我就先用89 手算一下兒。

f(x)=sin(x)
If x0 = 89
x1=x0-f(x0)/f(x0) = 89 – sin(89)/cos(89) = 31.7013

x2= x1 – f(x1)/f(x1)= 31.7013 – sin(31.7013)/cos(31.7013) = 31.0836
…………
 
花籃
分享
_________________
沒有希望的人生是無為的人生;充滿希望的人生是無畏的人生
沙發 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
router
(只看此人)



文章 時間: 2010-9-18 01:27 引用回復
哈哈,還有人像我一樣辛苦, @麽晚都不睡? icon_sad.gif

謝謝版主的花!

如果有可能的話,我很想聼聼你的看法和建議, 什麽槗懋夾校瑳]有對錯, 不必有顧慮。

阿牛,你也是coding高手,不要對嗡噁你是餐館端槃子的。。。。
 
花籃
分享
_________________
沒有希望的人生是無為的人生;充滿希望的人生是無畏的人生
板凳 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2010-9-18 02:03 引用回復
did you try google?

is it your assignment? or technical interview test? or other?

btw, I can't fully understand your questions, ie. what is "sin(x) + 1 cos(x)"?
 
花籃 (1)
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
地板 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2010-9-18 02:07 引用回復
google "Newton Raphson, C++", you'll find many clues
 
花籃 (1)
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
5 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2010-9-18 02:10 引用回復
example:


01 #include <iostream>
02 #include <iomanip>
03 #include <math.h>
04 #include <complex>
05
06 using namespace std;
07
08 //----------------------------------------------------------------------------//
09 // Function template: Newton-Raphson method find a root of the equation f(x) //
10 // see en.wikipedia.org/wiki/...n's_method //
11 // Parameters in: &x reference to first approximation of root //
12 // (&f)(x) reference to function f(x) //
13 // (fdiv)(x) reference to function f'(x) //
14 // max_loop maxiumn number of itterations //
15 // accuracy required accuracy //
16 // out: &x return root found //
17 // function result: > 0 (true) if root found, 0 (false) if max_loop exceeded //
18 template <class T1>
19 int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
20 int max_loop, const double accuracy)
21 {
22 T1 term;
23 do
24 {
25 // calculate next term f(x) / f'(x) then subtract from current root
26 term = (f)(x) / (fdiv)(x);
27 x = x - term; // new root
28 }
29 // check if term is within required accuracy or loop limit is exceeded
30 while ((abs(term / x) > accuracy) && (--max_loop));
31 return max_loop;
32 }
33
34 //----------------------------------------------------------------------------//
35 // test functions
36 double n, a;
37 double func_1(double x)
38 { return pow(x, n) - a; } // f(x) = x^n - a = 0
39
40 double fdiv_1(double x)
41 { return pow(x, n-1)*n; } // f'(x) = n * x^(n-1)
42
43
44 //----------------------------------------------------------------------------//
45 // Main program to test above function
46 int main()
47 {
48 cout << "\nFind root of f(x) = x^n - a = 0\n";
49 cout << "enter n and a ? ";
50 cin >> n >> a;
51 double x = 1.0; // initial 'guess' at root
52 if (newton(x, func_1, fdiv_1, 100, 1.0e-icon_cool.gif)
53 cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
54 else cout << "\n failed to find root ";
55 system("pause");
56 return 0;
57 }
 
花籃 (1)
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
6 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
router
(只看此人)



文章 時間: 2010-9-18 08:54 引用回復
webdriver 寫道:
did you try google?

is it your assignment? or technical interview test? or other?

btw, I can't fully understand your questions, ie. what is "sin(x) + 1 cos(x)"?



It's my technical interview test.

Based on the original question, my understanding is as following:

sin(x)+1 is the function
cos(x) is the derivative

Also
sin(x)+2 is the function
cos(x) is the derivative


cos(x) is the function
-sin(x) is the derivative

sin(x) is the function
cos(x) is the function

They are in pairs.


One thing which I am not sure about is:

What's the relationship between a function and its derivative or derivatives?
 
花籃
分享
_________________
沒有希望的人生是無為的人生;充滿希望的人生是無畏的人生


上一次由router於2010-9-18 09:17修改,總共修改了1次
7 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
router
(只看此人)



文章 時間: 2010-9-18 09:11 引用回復
webdriver 寫道:
example:


01 #include <iostream>
02 #include <iomanip>
03 #include <math.h>
04 #include <complex>
05
06 using namespace std;
07
08 //----------------------------------------------------------------------------//
09 // Function template: Newton-Raphson method find a root of the equation f(x) //
10 // see en.wikipedia.org/wiki/...n's_method //
11 // Parameters in: &x reference to first approximation of root //
12 // (&f)(x) reference to function f(x) //
13 // (fdiv)(x) reference to function f'(x) //
14 // max_loop maxiumn number of itterations //
15 // accuracy required accuracy //
16 // out: &x return root found //
17 // function result: > 0 (true) if root found, 0 (false) if max_loop exceeded //
18 template <class T1>
19 int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
20 int max_loop, const double accuracy)
21 {
22 T1 term;
23 do
24 {
25 // calculate next term f(x) / f'(x) then subtract from current root
26 term = (f)(x) / (fdiv)(x);
27 x = x - term; // new root
28 }
29 // check if term is within required accuracy or loop limit is exceeded
30 while ((abs(term / x) > accuracy) && (--max_loop));
31 return max_loop;
32 }
33
34 //----------------------------------------------------------------------------//
35 // test functions
36 double n, a;
37 double func_1(double x)
38 { return pow(x, n) - a; } // f(x) = x^n - a = 0
39
40 double fdiv_1(double x)
41 { return pow(x, n-1)*n; } // f'(x) = n * x^(n-1)
42
43
44 //----------------------------------------------------------------------------//
45 // Main program to test above function
46 int main()
47 {
48 cout << "\nFind root of f(x) = x^n - a = 0\n";
49 cout << "enter n and a ? ";
50 cin >> n >> a;
51 double x = 1.0; // initial 'guess' at root
52 if (newton(x, func_1, fdiv_1, 100, 1.0e-icon_cool.gif)
53 cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
54 else cout << "\n failed to find root ";
55 system("pause");
56 return 0;
57 }



Yes, I searched on the internet too and following is one sample that I found:

// Newton-Raphson method of finding roots //
// Passing references to functions f(x) and f'(x) as function parameters //
// also demonstrates use of a function template //

#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>

using namespace std;

//----------------------------------------------------------------------------//
// Function template: Newton-Raphson method find a root of the equation f(x) //
// see en.wikipedia.org/wiki/...n's_method //
// Parameters in: &x reference to first approximation of root //
// (&f)(x) reference to function f(x) //
// (fdiv)(x) reference to function f'(x) //
// max_loop maxiumn number of itterations //
// accuracy required accuracy //
// out: &x return root found //
// function result: > 0 (true) if root found, 0 (false) if max_loop exceeded //
template <class T1>
int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
int max_loop, const double accuracy)
{
T1 term;
do
{
// calculate next term f(x) / f'(x) then subtract from current root
term = (f)(x) / (fdiv)(x);
x = x - term; // new root
}
// check if term is within required accuracy or loop limit is exceeded
while ((abs(term / x) > accuracy) && (--max_loop));
return max_loop;
}

//----------------------------------------------------------------------------//
// test functions

double func_1(double x) // root is 1.85792
{ return (cosh(x) + cos(x) - 3.0); } // f(x) = cosh(x) + cos(x) - 3 = 0

double fdiv_1(double x)
{ return (sinh(x) - sin(x)); } // f'(x) = sinh(x) - sin(x)

double func_2(double x) // root is 5.0
{ return (x*x - 25.0); } // f(x) = x * x - 25 = 0

double fdiv_2(double x)
{ return (2.0 * x); } // f'(x) = 2x

complex<double> func_3(complex<double> x) // roots 5 + or - 3
{ return x*x - 10.0*x + 34.0; } // f(x) x^2 - 10x + 34

complex<double> fdiv_3(complex<double> x)
{ return 2.0*x -10.0; } // f'(x) 2x - 10

double func_4(double x) // three real roots 4, -3, 1
{ return 2*x*x*x - 4*x*x - 22*x + 24 ; } // f(x) = 2x^3 - 4x^2 - 22x + 24

double fdiv_4(double x)
{ return 6*x*x - 8*x - 22; } // f'(x) = 6x^2 - 8x - 22

//----------------------------------------------------------------------------//
// Main program to test above function
int main()
{
cout << "\nFind root of f(x) = cosh(x) + cos(x) - 3 = 0";
double x = 1.0; // initial 'guess' at root
if (newton(x, func_1, fdiv_1, 100, 1.0e-icon_cool.gif)
cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
else cout << "\n failed to find root ";

cout << "\n\nFind root of f(x) = x * x - 25 = 0";
x = 1.0; // initial 'guess' at root
if ( newton(x, func_2, fdiv_2, 100, 1.0e-icon_cool.gif)
cout << "\n root x = " << x << ", test of f(x) = " << func_2(x);
else cout << "\n failed to find root ";

cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
complex<double> xc = complex<double>(1.0, 1.0); // initial 'guess' at root
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-icon_cool.gif)
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
else cout << "\n failed to find root ";

cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
xc = complex<double>(1.0, -1.0); // initial 'guess' at root
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-icon_cool.gif)
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
else cout << "\n failed to find root ";

cout << "\n\nFind root of f(x) = 2x^3 - 4x^2 - 22x + 24 = 0";
x = 5.0; // initial 'guess' at root
if ( newton(x, func_4, fdiv_4, 100, 1.0e-icon_cool.gif)
cout << "\n root x = " << x << ", test of f(x) = " << func_4(x);
else cout << "\n failed to find root ";

cin.get();
return 0;
}

I can see your sample code and my sample code are very similar except the format of functions and corresponding derivatives. But how do they related to my original task:

Find a root of the following functions (where x is in radians):

Function
sin(x)
Derivative
cos(x)

Function
cos(x)
Derivative
-sin(x)

Function
sin(x) + 1
Derivative
cos(x)

Function
sin(x) + 2
Derivative
cos(x)

?????
 
花籃
分享
_________________
沒有希望的人生是無為的人生;充滿希望的人生是無畏的人生
8 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
7thGuest
(只看此人)



文章 時間: 2010-9-18 10:53 引用回復
代碼:

#include <cstdio>
#include <cmath>
const double epsilon = 0.0000001;
const double delta = 0.0000001;
typedef double (*FUNC)(double x);
double NR_Eval(FUNC f,double initial=0)
{
   double x = initial;
   do
   {
      double fx = f(x);
      if(fx<epsilon&&fx>-epsilon)
         break;
      x = x - fx/
         ((f(x+delta)-f(x-delta))/(delta*2));
   }while(1);
   return x;
}

double Test1(double x)
{return sin(x)*cos(x);}
double Test2(double x)
{return cos(x) - sin(x);}
double Test3(double x)
{return x*x+2*x+1;}


int main()
{
   double d = NR_Eval(Test3);
   printf("%f",d);
   return 0;
}


 
花籃 (10)
分享
_________________
烈焰空燒吾秘授
9 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
7thGuest
(只看此人)



文章 時間: 2010-9-18 10:55 引用回復
test the boundary case your self

big_happy.gif
 
花籃
分享
_________________
烈焰空燒吾秘授
10 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
 
回復主題     |##| -> |=|     論壇首頁 -> IT人生 所有的時間均為 美國太平洋時間
1頁,共7 分頁: 1, 2, 3, 4, 5, 6, 7  下一頁  


注:
  • 以上論壇所有發言僅代表發帖者個人觀點, 並不代表本站觀點或立場, 加西網對此不負任何責任。
  • 投資理財及買房賣房版面的帖子不構成投資建議。投資有風險,責任請自負
  • 對二手買賣中的虛假信息,買賣中的糾紛等均與本站無關。
  • 黃頁熱門商家 免費個人廣告
    發布商業廣告

    不能在本論壇發表新主題
    不能在本論壇回復主題
    不能在本論壇編輯自己的文章
    不能在本論壇刪除自己的文章
    不能在本論壇發表投票
    不能在這個論壇添加附件
    可以在這個論壇下載文件

    論壇轉跳: 

    router, router, router, webdriver, webdriver, webdriver, router, router, 7thGuest, 7thGuest
    潛力帖子 精華帖子 熱門帖子
    性無能者的權力扮演:虐待、賴賬與...
    求大家給個建議 !
    加拿大
    尹科就一詐騙犯,遲早把自己玩死!
    從溫哥華列治文發出來的包裹到達多...
    中國的一萬億美元順差,給普通民眾...
    Boxing Day 大家都買什麼好東東了?
    今天洗牙心得
    俄烏之戰,古巴導彈危機,和武統台灣。
    正式啟動彈劾程序:賴清德被提案彈劾!
    堅決不吃這個 死貓
    不順心了,就去懟一懟AI
    回應對台軍售 中國宣布制裁美20家...
    加西名詩欣賞 ------ 爹戀花 憶劉廳...
    老強種!!!跟加西盤子劉真特麼像!
    邁阿密(四)勞德代爾堡
    邁阿密(三)Key West
    一夜消失! 加拿大這家華人超市突然...
    邁阿密(二)大沼澤地 維資卡亞 海...
    邁阿密(一)南沙灘 小哈瓦那 溫伍...
    加拿大全國各地兌換紀念【無名烈士...
    2025紀念無名烈士加拿大2元流通硬幣
    自藏求精!
    西岸快線30周年紀念品
    天津深度游(二)
    天津深度游
    mint十月新幣 (十一月新幣從25樓開始)
    魁北克 水晶瀑布 加國航拍
    舌尖上的預制菜!
    游了一下多倫多(三)多倫多群島 湖...
    本宮鋼琴彈奏原聲第1彈 一首前奏曲
    謝謝管理員秉公執法廢除reddragon的id
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老流氓劉廳...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...

    最新新聞 熱門新聞 熱評新聞
    AI已經能暢玩幾乎所有游戲了,那還要我幹啥?
    川普戰列艦讓敵人瑟瑟發抖?還是額手稱慶?
    別再用!輪胎店熱賣商品被點名召回
    "美國斬殺線"爆火 它真的存在嗎?又在忽悠哪些人
    暴雨成災!加拿大更新赴美安全提示
    2上將1中將官宣落馬 何衛東遭罷人大職務
    全球前50大經濟體排名出爐!美國居冠陸第二
    Costco柳橙為何容易發霉?外媒揭背後致命原因
    南下注意:出入境美國要“刷臉”了!
    張雨生的身亡,其實不是醉駕,飆車那麼簡單
    孟晚舟卸任大反轉 華為的野心暴露了
    毛新宇當上了副部長,釋放了什麼重要信號?
    美國人在買單:川普關稅風暴席卷全球的一年
    北京市人社局仲裁:以AI替代崗位解雇員工屬違法
    突發:美軍決定留下!北京用力過猛?
    每天穿破損羽絨服12小時,男子呼吸衰竭搶救
    豆瓣9.2 :一位名校教授記錄的青少年抑郁真相
    阿迪達斯變中迪達斯,"這丑衣服到底誰愛穿"
    李湘 "鋪路"過頭 被郭晶晶何超瓊將體面撕碎
    美國2.4萬航班取消或延誤 紐約州進入緊急狀態
    今天的銀發經濟,有一個巨大的供需錯配
    為啥5元流感疫苗打不動,200元流感藥搶著要?
    國內資產被法拍 日本住豪宅 王思聰瀟灑的背後
    因多次辱罵、恐嚇他人 一民警被控尋釁滋事罪
    歐豪發三大喜訊竟暗自追隨謝霆鋒的腳步
    被官方約談後,郭德綱收到天津和平區文旅局邀請
    《罰罪2》空降,兄弟反目!黃景瑜、王傳君上演黑白雙王對決
    「沒有人在」!張柏芝自曝剖腹產返家傷口發炎
    印度正在為與中國發生沖突做准備
    罰罪2:黃景瑜演技被張桐吊打,卻贏在長相,男明星顏值太重要
    余茂春致信美國精英:別對中共抱有幻想
    聖誕夜北溫登山迷路 志願者成功救援
    蔡奇蔫了:兩場紅色愛國大戲,竟被他出來攪局
    川普核心顧問:移民後代是問題 無法融入美國
    美國2.4萬航班取消或延誤 紐約州進入緊急狀態
    李湘 "鋪路"過頭 被郭晶晶何超瓊將體面撕碎
    阿迪達斯變中迪達斯,"這丑衣服到底誰愛穿"
    豆瓣9.2 :一位名校教授記錄的青少年抑郁真相
    2026北美車展最佳車款名單出爐 9款新車亮相
    今天的銀發經濟,有一個巨大的供需錯配
    為啥5元流感疫苗打不動,200元流感藥搶著要?
    每天穿破損羽絨服12小時,男子呼吸衰竭搶救
    國內資產被法拍 日本住豪宅 王思聰瀟灑的背後
    因多次辱罵、恐嚇他人 一民警被控尋釁滋事罪
    歐豪發三大喜訊竟暗自追隨謝霆鋒的腳步

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


    Powered by phpBB 2.0.8
    Terms & Conditions    Privacy Policy    Political ADs    Activities Agreement    Contact Us    Sitemap    

    加西網為北美中文網傳媒集團旗下網站

    頁面生成: 0.0451 秒 and 7 DB Queries in 0.0016 秒