Gooman
Gooman

Reputation: 175

Matlab - selection of numbers for equation

I trying to select numbers in order to solve the equation. Here is my code:

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

This code does not work properly. Values p1, p2, p3, y0_star, y02_2, y03_2 should be in the range [0, 1]. And p1 + p2 + p3 should equals 1. Also I want y0_star < 0.5. How I can do it? It does not means that every selected number should be random. I also tried to find some information about solving linear equations in Matlab. But I found information for only systems of equations.

Upvotes: 0

Views: 48

Answers (1)

rayryeng
rayryeng

Reputation: 104504

The main reason why this isn't working is because you're not ensuring that p1 + p2 + p3 = 1. What you should do is when you find p1, p2 and p3, you should divide each of these constants by the sum of all these numbers. This will ensure that p1 + p2 + p3 = 1. With this, you can eliminate the checks to see if the values sum to 1. As such, this is your modified code where I have indicated where you need to change:

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

Running the above code, this is what I get for all of the variables:

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

Note that you will get different results as these numbers are randomly generated.


Edit per your comments

You want to modify the code such that p1 > p2 > p3. Simply place an if statement inside your else statement that checks for this fact when you are generating p1,p2,p3. If not, then reset all of the parameters back to the default then try again.

In other words:

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

When I do this, this is what I get:

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

Upvotes: 1

Related Questions