Matlab - 方程式的数字选择

时间:2014-10-03 04:01:29

标签: matlab equation

我试图选择数字以解决方程式。这是我的代码:

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&lt; 0.5。我怎么能这样做?这并不意味着每个选定的数字都应该是随机的。我还试图找到一些关于在Matlab中求解线性方程的信息。但我只找到了方程组的信息。

1 个答案:

答案 0 :(得分:1)

这不起作用的主要原因是因为你没有确保p1 + p2 + p3 = 1。您应该做的是当您找到p1p2p3时,您应该将这些常数中的每一个除以所有这些数字的总和。这将确保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。只需在if语句中放置else语句,即在生成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