2014-10-03 38 views
0

我試圖選擇數字來解決方程。以下是我的代碼:Matlab - 爲方程選擇數字

clc; 
clear; 
y0_star = 1; 
p1 = 1; 
p2 = 1; 
p3 = 1; 
y01_2 = 0; 
y02_2 = 0; 
y03_2 = 0; 
iter_cnt = 0; 
while (y0_star > 0.5) && (p1 + p2 + p3 ~= 1) 
    y0_star = 1 - 1/(p1/(1 - y01_2) + p2/(1 - y02_2) + p3/(1 - y03_2)); 

    if (y0_star < 0.5) && (p1 + p2 + p3 == 1) 
     disp('no errors') 
     break 
    else 
     p1 = rand; 
     p2 = rand; 
     p3 = rand; 
     y01_2 = rand; 
     y02_2 = rand; 
     y03_2 = rand; 
    end 

    iter_cnt = iter_cnt + 1; 
    if iter_cnt > 10^6 
     disp('error') 
     break 
    end 
end 

此代碼無法正常工作。 值p1,p2,p3,y0_star,y02_2,y03_2應該在[0,1]的範圍內。並且p1 + p2 + p3應該等於1.同樣我想要y0_star < 0.5。我如何做到這一點?這並不意味着每個選定的數字都應該是隨機的。我也試圖找到一些關於在Matlab中求解線性方程的信息。但是我發現只有方程組的信息。

回答

1

這不工作的主要原因是因爲你不能確保p1 + p2 + p3 = 1。你應該做的是當你發現p1,p2p3,你應該將這些常數除以所有這些數字的總和。這將確保p1 + p2 + p3 = 1。有了這個,你可以消除檢查,看是否值總和爲1。因此,這是修改後的代碼,我已經表明,你需要改變:

clc; 
clear; 
y0_star = 1; 
p1 = 1; 
p2 = 1; 
p3 = 1; 
y01_2 = 0; 
y02_2 = 0; 
y03_2 = 0; 
iter_cnt = 0; 
while (y0_star > 0.5) %// Change - no need to check for sum not equaling to 1 
    y0_star = 1 - 1/(p1/(1 - y01_2) + p2/(1 - y02_2) + p3/(1 - y03_2)); 

    if (y0_star < 0.5) %// Change - no need to check if sum equals 1 since we're normalizing 
     disp('no errors') 
     break 
    else 
     p1 = rand; 
     p2 = rand; 
     p3 = rand; 
     s = p1 + p2 + p3; %// Change - Find sum of p1 + p2 + p3 
     p1 = p1/s; %// Change - Divide by sum to ensure sum is 1 
     p2 = p2/s; 
     p3 = p3/s; 
     y01_2 = rand; 
     y02_2 = rand; 
     y03_2 = rand; 
    end 

    iter_cnt = iter_cnt + 1; 
    if iter_cnt > 10^6 
     disp('error') 
     break 
    end 
end 

運行上面的代碼,這是什麼我得到所有的變量:

p1 = 

    0.3114 

p2 = 

    0.3476 

p3 = 

    0.3409 

y01_2 = 

    0.3922 

y02_2 = 

    0.6555 

y03_2 = 

    0.1712 

y0_star = 

    0.4826 

iter_cnt = 

    3 

請注意,您將得到不同的結果,因爲這些數字是隨機生成的。按您的意見


編輯您要修改的代碼,這樣p1 > p2 > p3。只需在您的else聲明中添加if聲明,該聲明在您生成p1,p2,p3時檢查這一事實。如果不是,則將所有參數重置爲默認值,然後重試。

換句話說:

clc; 
clear; 
y0_star = 1; 
p1 = 1; 
p2 = 1; 
p3 = 1; 
y01_2 = 0; 
y02_2 = 0; 
y03_2 = 0; 
iter_cnt = 0; 
while (y0_star > 0.5) %// Change - no need to check for sum not equaling to 1 
    y0_star = 1 - 1/(p1/(1 - y01_2) + p2/(1 - y02_2) + p3/(1 - y03_2)); 

    if (y0_star < 0.5) %// Change - no need to check if sum equals 1 since we're normalizing 
     disp('no errors') 
     break 
    else 
     p1 = rand; 
     p2 = rand; 
     p3 = rand; 
     %// Change - as per your comment 
     if (~((p1 > p2) && (p2 > p3)))    
      y0_star = 1; 
      p1 = 1; 
      p2 = 1; 
      p3 = 1; 
      y01_2 = 0; 
      y02_2 = 0; 
      y03_2 = 0; 
      continue; 
     end   
     s = p1 + p2 + p3; %// Change - Find sum of p1 + p2 + p3 
     p1 = p1/s; %// Change - Divide by sum to ensure sum is 1 
     p2 = p2/s; 
     p3 = p3/s; 
     y01_2 = rand; 
     y02_2 = rand; 
     y03_2 = rand; 
    end 

    iter_cnt = iter_cnt + 1; 
    if iter_cnt > 10^6 
     disp('error') 
     break 
    end 
end 

當我做到這一點,這就是我得到:

p1 = 

    0.5417  

p2 = 

    0.4470 

p3 = 

    0.0113 

y01_2 = 

    0.3371 

y02_2 = 

    0.1622 

y03_2 = 

    0.7943 

y0_star = 

    0.2886 

iter_cnt = 

    3 

+0

謝謝你,它的工作原理。還有一個問題。我可以做些什麼來使p1> p2> p3? – Gooman 2014-10-03 04:22:03

+0

@Gooman - 是的,這是可能的。我會做一個編輯。 – rayryeng 2014-10-03 04:25:40

+0

@Gooman - 完成。祝你好運! – rayryeng 2014-10-03 04:41:36