From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How can I find the mean between to points of normal distribution?
Date: Sun, 15 Aug 2010 19:50:04 +0000 (UTC)
Organization: FIU
Lines: 30
Message-ID: <i49gdc$7fj$>
References: <i371ts$ht3$> <i37711$fqu$> <i37ctu$1ta$> <i37he8$cmp$> <i394bk$5c$> <i39ms6$gph$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1281901804 7667 (15 Aug 2010 19:50:04 GMT)
NNTP-Posting-Date: Sun, 15 Aug 2010 19:50:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2433679
Xref: comp.soft-sys.matlab:662228

After my first tests I thought everything was working correctly, but it seems that it is not.  I have attached a test script whereas it calculates the expected value with a brute
force way, and a way using the formula you stated in your reply.  It calculates these 2 values for varying values of mu.  You can see from the plot, that the green line which represents your formula, goes outside the min/max.  I probably just have a sign incorrect or
something, so if you could take a look I would really appreciate it.

function [ ] = testExpectedValue()

    a = 25;
    b = 26;
    sigma = 1;
    mean1(1) = 0;
    mean2(1) = 0;
    for mu=20:.1:30
        x = random('normal',mu,sigma,1000000,1);
        mean1(end+1) = mean(max(a,min(b,x)));
        mean2(end+1) = a*normcdf(a,mu,sigma) + sigma^2*(normpdf(a)-normpdf(b)) + ...
            mu*(normcdf(b,mu,sigma) - normcdf(a,mu,sigma)) + ...
    close all;
    plot(mean1(2:end),20:.1:30);hold on;  plot(mean2(2:end),20:.1:30,'g');