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

首頁

溫哥華資訊

溫哥華地產

溫哥華教育

溫哥華財稅

新移民/招聘

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: XY | 細雨飄渺 | 亂想 | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 五木森林 | 羅蓬特機器人 | 豬頭看世界 | Invisible world | 靜觀雲卷雲舒 | 真情Z下海 | sasa0f | 大溫房產和地產研究 | simajibr1 | 顧曉軍 | dwx | dumpling0 | Calm zone | 呱呱叫廚房 | Amy Yi | My AI Tech Channel
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿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
    潛力帖子 精華帖子 熱門帖子
    美國在川普的進攻下,民主政體節節敗退
    英媒:美軍已制定強攻哈爾克島計劃...
    中共國軍事科技人員伊朗秘密參戰
    同仇敵愾還是針鋒相對?
    外星人交流一下:【吃虧是福】!
    看了不太舒服
    伊朗的山
    投懷送抱
    今冬蘋果
    紅瓤的橙子
    計劃去中國的老外
    國內吃的好便宜
    送走100多位黨和國家領導人,戰事漸...
    小鳥依人人丑就做怪
    你曾經暗戀的日本情人
    奧蘭多(二)迪士尼動物王國
    奧蘭多(一)
    Royal Canadian Mint 2026年2月新幣
    坎昆(一)
    推薦一個digital的手持放大鏡
    RCM 2026年1月新幣
    拆卷有驚喜
    爐關尋覓記
    邁阿密(四)勞德代爾堡
    邁阿密(三)Key West
    一夜消失! 加拿大這家華人超市突然...
    邁阿密(二)大沼澤地 維資卡亞 海...
    邁阿密(一)南沙灘 小哈瓦那 溫伍...
    加拿大全國各地兌換紀念【無名烈士...
    2025紀念無名烈士加拿大2元流通硬幣
    特朗普又幹了件冒天下之大不韙的事...
    本宮鋼琴彈奏原聲第1彈 一首前奏曲
    謝謝管理員秉公執法廢除reddragon的id
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老流氓劉廳...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣

    最新新聞 熱門新聞 熱評新聞
    斯琴高娃:76歲的她還要為了兒女去拍戲....
    45歲柳岩近況曝光!被傳與大學初戀復合歸隱田園
    蔣雯麗:剛打贏桃色官司,又遭人惡意編排
    《隱身的名字》14集過後,揭了時代的傷疤,堪稱偉大的女性劇
    熱播劇《逐玉》全集大規模泄露,嚴正聲明能否止住侵權亂象?
    扎克伯格又收編一AI智能體公司 前小米副總裁回歸
    特斯拉招募資深芯片工程師 需十年以上開發經驗
    七部年代劇轟炸,《冬去春來》靠什麼讓觀眾不換台?
    浮光錦:娛樂圈造神中的文化龐氏騙局
    血色浪漫,權與欲的絕美宮廷劇,這六部私藏每一部都是珍品
    過億熱度刷新行業紀錄:拆解閱文短劇《隱身侍衛》的"人味兒"爆款學
    美伊消息,引發全球市場巨震.....
    因反戰辭職美高官:必須遏制以色列,否則陷入怪圈
    美媒:美方考慮出動第82空降師,支援對伊作戰
    陪玩陪睡只是冰山一角?張藝謀團隊曝猛料
    12歲長這樣?"小學生辣妹"畢業照引百萬人朝聖
    隨機槍殺亞裔孕婦 凶手被判"精神失常不負刑責"
    "一天沒找到梅姨 心都放不下來" 兩個父親熬白了頭…
    六州總檢察長敦促打擊為芬太尼洗錢的微信活動
    學術期刊研究"兒童如廁" 專家:倫理層面明顯缺失
    美公司拒孕婦居家辦公 導致嬰兒夭折…判賠$2250萬
    克拉拉擦邊香艷床戲包貝爾再演壞淫!這部網大爽得太膚淺
    卷飛了的沈陽澡堂子,爆改好萊塢片場!
    油價成"隱形稅"!這次,我們低估油價的影響了
    不是中俄古,川普透露伊朗之後下1個最大敵人
    中國公司承認8艘船被困波斯灣 網民嘲諷
    "最羞辱的體驗":加拿大Aritzia被罵爆
    好市多搶瘋了!研究曝冷凍藍莓好處多
    芝女大生散步突遭非法移民爆頭 嫌犯曾兩度被釋
    哥倫比亞一架C130"大力神"運輸機墜毀 載員125人
    摩薩德忍無可忍 對中共發出嚴重警告
    央媒官宣章子怡新身份引爆全網 釋放三個強烈信號
    中伊卡住全球脖子!博弈"三件套"前所未有
    53歲李冰冰近況曝光:狀態絕佳 像35歲
    中國公司承認8艘船被困波斯灣 網民嘲諷
    加國老人中國昏迷付不起住院費:眾籌包機返加
    大溫油價飆至2.14/升 還會漲多高?
    正副機師喪命!加航司飛機紐約機場撞上消防車
    "最羞辱的體驗":加拿大Aritzia被罵爆
    運輸機墜毀 傳約80軍人據信罹難
    隨機槍殺亞裔孕婦 凶手被判"精神失常不負刑責"
    "一天沒找到梅姨 心都放不下來" 兩個父親熬白了頭…
    身邊很多人,根本就不能算是一個現代人
    六州總檢察長敦促打擊為芬太尼洗錢的微信活動
    學術期刊研究"兒童如廁" 專家:倫理層面明顯缺失

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

    頁面生成: 0.0643 秒 and 7 DB Queries in 0.0017 秒