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

首頁

溫哥華資訊

溫哥華地產

溫哥華教育

溫哥華財稅

新移民/招聘

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: XY | NotmeL8 | 格局 | 羅蓬特機器人 | 豬頭看世界 | lxls | 禪人俗事 | 顧曉軍 | 一襲絳襦落鵬城,疑似玄女下九天 | 呂洪來的個人空間 | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 白龍王許道長 | 湖裡湖塗 | 天涯逐夢 | My AI Tech Channel | Invisible world | 我的退休生活 | 逸言堂 | 真情Z下海 | 大溫房產和地產研究
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿PR卡未入籍的永久居民會被強制服兵役嗎?   這個銀條   如何修改會員名?
 論壇轉跳:
     發帖回帖獲取加西鎊, 兌換精彩禮物

論壇首頁 -> IT人生

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

分頁: 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
    潛力帖子 精華帖子 熱門帖子
    完了,加國房東們的災星上台了
    據說ndp溫東那位女議員要投靠自由黨
    一大早起來感覺還挺涼的
    音頻: 一中國大爺把小兵短脖粉紅奴...
    劉廳長和豬大福,小兵老狼路人行華...
    加州州長:我們不是美國,對中國伸...
    蘆筍好貴
    今天終於把稅報了
    西洋老歌。五百二十二
    美國商會喊話特朗普:趕緊減免關稅...
    中國說,關稅降到0,否則一切免談!...
    加西第一蠢貨推薦/提名貼
    卡尼當選第一次記者會還是很有新意滴
    網民調侃外交部那個義和團巨嬰耍潑...
    上海大破產!重慶也跟著一起?中國...
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    大漢四川軍政府軍用銀票
    今年新幣發行計劃
    要出一個新的一元
    古董金幣
    mint三月新幣(四月新幣從22樓起)
    1999 mule 25分
    2025 蛇年敲幣活動
    加拿大新總理馬克卡尼
    我在小紅書被罵窮得沒錢給孩子買衣服
    美國2025年AWQ(美國婦女25c)發行計劃
    韓國空難FDR黑匣子缺失最後四分鍾關...
    又見假幣
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?
    退休幾年後的感悟

    最新新聞 熱門新聞 熱評新聞
    美134平米住宅標價20萬美元 設計奇葩引爆網絡
    全紅嬋老家蓋新房 將建起120平米別墅 宅基地系獎勵
    突發 菲沙河船只起火有毒煙霧飄散
    SpaceX星際基地公投在即 馬斯克能否擁有自己的城?
    避風港被毀,"這裡的中國公司很害怕"
    義烏最牛老板娘:美客戶很重要 但此路不通還有別路
    美加航空需求量大降 暫停部分航班
    不靠燙染 哈佛教授3招秘訣逆轉白發
    伯克希爾股東大會開幕 94歲巴菲特重磅發聲
    耳垢中的健康密碼:幹濕和氣味如何揭示各種疾病
    Siri升級?傳蘋果內部正在測試iOS19.4版本
    德國選項黨被定為極右翼 美國務卿怒斥"暴政"
    德國馬堡汽車撞人襲擊案嫌犯被拘留 更多細節曝光
    耐克、阿迪達斯等品牌請求特朗普豁免鞋類關稅
    恐怖!重慶男當街縱火砍人 再割喉自殺
    查爾斯國王這天訪加&發表施政演說
    馬自達五月租賃利息最低可至1.35%*
    特朗普政府大裁員持續:中情局1200人也未能幸免
    重大車禍2人死 中國網絡急封關鍵詞刪影片
    中國正考慮解決美國對芬太尼的擔憂 為貿易談判提供機會
    舍不得中國市場 傳英偉達這樣繞過出口限制
    葛優交給妻子350萬 大贊妻子轉身就花光
    美國太空軍衛星近距離"檢查"2顆中共軍事衛星
    局勢再升級!印度宣布禁止與巴基斯坦的進口貿易
    特朗普出訪沙特前,美國務院批准35億美元導彈軍售
    5死8傷!中國公民在美遭遇事故,原因正在調查之中
    美政府"無人可用"?國務卿身兼四職,或再接防長...
    "海湖莊園協議"傳出 恐引發全球金融海嘯
    在歐盟TikTok被罰5.3億歐,在美國TikTok背負"不賣就禁"
    馬斯克承認未能實現削減政府開支目標
    溫村街頭死傷賠償 ICBC新政成難題
    快撐不住了? 中國對美關稅豁免價值400 億美元
    1年僅開2月大溫"莫奈花園"周六開放
    今天北上每車都查 大溫入境排長龍
    溫村街頭慶典襲擊案 嫌犯今天受審
    劃算!大溫這連鎖餐廳下周推$5美食
    卡尼勝選後首次講話 幾條消息驚人
    中國直升機墜毀 "全機存活" 砸死地上路人
    求川普豁免關稅 Nike等76品牌緊急聯署
    重大車禍2人死 中國網絡急封關鍵詞刪影片
    中國正考慮解決美國對芬太尼的擔憂 為貿易談判提供機會
    舍不得中國市場 傳英偉達這樣繞過出口限制
    葛優交給妻子350萬 大贊妻子轉身就花光
    美國太空軍衛星近距離"檢查"2顆中共軍事衛星
    局勢再升級!印度宣布禁止與巴基斯坦的進口貿易

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

    頁面生成: 0.0643 秒 and 5 DB Queries in 0.0018 秒