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

首頁

溫哥華資訊

溫哥華地產

溫哥華教育

溫哥華財稅

新移民/招聘

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: 真情Z下海 | XY | 豬頭看世界 | 亂想 | lxls | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 羅蓬特機器人 | NotmeL8 | 一襲絳襦落鵬城,疑似玄女下九天 | 白龍王許道長 | 五木森林 | Amy Yi | 顧曉軍 | 異鄉的世界 | 湖裡湖塗 | 靜觀雲卷雲舒 | STEVEN90 | 呂洪來的個人空間 | Invisible world | 呱呱叫廚房
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿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
    潛力帖子 精華帖子 熱門帖子
    哭吧哭吧 溫東小兵 武統台灣已無可能
    巴西慘中50%關稅 金磚國家關稅30%起跳
    愛潑斯坦蘿莉島名單再起風雲
    USAid 在六月30號正式關張。。。。。
    加拿大搞自己的稀土
    將美國驅逐出去,將清楚地表明其作...
    億萬富姐劉偉宏真能讓The Bay 再次...
    ______海參崴被炸
    敢侵台就轟炸北京,川普發言曝光
    賴清德啟程訪美 美國公開打台灣牌
    美國人用鞋底子證明了:美國今天的...
    床鋪剛硬了一回
    蔡總統1.5個博士之謎! 彭文正潛入倫...
    翟山鷹:中共高層已經形成共識 放棄...
    今天AmazonPrimeDay, 你們都買啥了?
    加拿大海洋三省狂奔游 (三) New Bru...
    加拿大海洋三省狂奔游(二)Nova scotia
    加拿大海洋三省狂奔游(一)PEI
    從阿壩州到甘孜州,穿越川西
    RCM 2025年6/7月新幣
    “五到八年後再看,疫情後的2023年...
    又看完一部電視劇
    新疆伊犁 賽裡木湖 三大草原恰西 喀...
    新疆阿勒泰 五彩灘 喀納斯 魔鬼城
    在烏魯木齊看娘娘騎過的汗血寶馬
    一張天主教在華發行紙鈔略考
    5月2日換幣盛況
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?

    最新新聞 熱門新聞 熱評新聞
    美股標普那指再創新高 英偉達首破4萬億美元
    大溫本周末活動 華埠節+太陽狂歡節
    本那比這周末免費戶外交響樂/歌劇
    BC省三家酒店評為加拿大最佳酒店
    騎電驢還玩手機? 北京開始嚴查(圖
    鵝蛋為什麼很少有人吃 也很少有賣
    駭人聽聞!華裔女涉長期性侵兒子肉體虐待女兒
    川普"橫插一腳" 美越關稅協議沒有談成?
    意大利應美國要求,抓捕來度蜜月的上海男人
    川普嗆普京"廢話一堆" 揚言炸莫斯科
    希望馬斯克的"反川大業"能成,雖然他成不了
    相識六年,她發現男友是政府監視她的秘密臥底
    震驚!德州發生武裝攻占移民監獄事件 亞裔嫌犯在逃
    對美強硬卻對北京沉默?石破茂遭"酸"爆
    轟動 大溫全新天車要上路民眾圍觀
    大陸首次制裁島內8大軍火商,是警告和算帳
    這一大塊月球玄武岩,藏著什麼月球奧秘?
    青海隱形首富北京千萬豪宅被法拍 同時獲刑6年半
    73歲三浦友和再破天花板 夫妻恩愛45載
    祖籍潮汕,泰國咖啡王陳保焜財富縮水,父女涉...
    90後外賣小哥從衡水騎電動車去拉薩,邊旅行邊...
    朱珠的中年穿衣法則:清爽色彩、醒目配飾
    杭州站臥軌自殺事件:好的人生,要懂"苦亦是空"
    IT男以Tesla為家住公園4年 坐擁海景每晚停車費6元
    對話"男友駕車出事致截癱"雙方:女生稱被花束...
    青海隱形首富北京豪宅被拍 到故宮僅15分鍾
    誰更有可能被人工智能取代:新手還是資深員工?
    上海IT工程師落地米蘭被逮捕,涉參與疫苗黑客案
    韓國男星文泰一性侵中國女子,一審獲刑3年半
    AI正越來越貴:馬斯克將訂閱費推高至300美元/月
    震驚!聯邦3年要省250億大裁員要來
    溫哥華擬1/3地區允建高層社會住房
    普京堅信烏克蘭即將總崩潰 夙願馬上實現
    盧比歐考慮在馬來西亞會晤王毅 美中互動備受關注
    阿爾茨海默病手術被叫停背後:曾有醫院稱100%有效
    正在熱播的3部爛劇,沒有最爛只有更爛,一部沒看真是慧眼識珠
    卡尼官宣砍開支 政府通知將大裁員
    中國外賣大戰:又一場"虛假繁榮"?
    2-0沖冠,鄭欽文"苦主"挑戰世界第一
    青海隱形首富北京千萬豪宅被法拍 同時獲刑6年半
    73歲三浦友和再破天花板 夫妻恩愛45載
    祖籍潮汕,泰國咖啡王陳保焜財富縮水,父女涉...
    90後外賣小哥從衡水騎電動車去拉薩,邊旅行邊...
    這一大塊月球玄武岩,藏著什麼月球奧秘?
    對話"男友駕車出事致截癱"雙方:女生稱被花束...

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

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