Results 1 to 2 of 2

Math Help - Need help with Matlab (Loops)

  1. #1
    Newbie
    Joined
    Feb 2011
    Posts
    1

    Need help with Matlab (Loops)

    I have 2 questions I need help with;


    Question 1:

    355 / 113 is a good approximation of pi. Let p and q be positive integer. Write a Matlab program to find the lowest value of q (and attached p) that meets the condition
    | p / q - pi | < | 355 / 113 - pi |


    Trying for a solution;

    First of all, 113 needs to be one step more (114). If we take pi away, we get a negative number which is a sign that we need to increase 355. Do this until we get positive numbers again (when we look at the new error at the new fraction), then we increase 355 with 1 (and so on). Everytime we change, we also look if abs(newerror) < abs(old error)

    code:

    function [t, n, err] = fracpi(t0, n0)

    e0 = t0/n0-pi;

    t = t0;
    n = n0;

    while (1)
    frac = t/n;
    if abs(frac-pi) < abs(e0), break, end
    if frac-pi < 0
    t=t+1;
    else
    n=n+1;
    end
    end

    err = frac-pi;
    end

    I thought this would be right. But Matlab tells its wrong.


    2 question;

    Let ndig(q) be the quantity of numbers in q, that ndig(519) = 3, ndig(3322) = 4 and so on, Write a Matlab program to calculate the best approximation, p/q, of exp(1) when ndig(q) = 1,2,3,4,5,6. If there is more (p, q) - pair (for a given value on ndig(q)) that gives the same value for the error, then write (p, q)-pair with the lowest value on q.

    Trying for solution;


    Pretty much the same concept as the other question.

    code:
    function [p, q, err] = frace(ndigq)

    lim = 10^ndigq - 1;

    t0 = 10^(ndigq-1);
    n0 = t0;

    exp1 = exp(1);
    e0 = t0/n0-exp1;

    t = t0;
    n = n0;

    while n<=lim
    err = t/n-exp1;
    if abs(err) < abs(e0)
    p = t;
    q = n;
    e0 = err;
    end
    if err < 0
    t=t+1;
    else
    n=n+1;
    end
    end

    err = p/q-exp1;
    end
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kaktusse View Post
    I have 2 questions I need help with;


    Question 1:

    355 / 113 is a good approximation of pi. Let p and q be positive integer. Write a Matlab program to find the lowest value of q (and attached p) that meets the condition
    | p / q - pi | < | 355 / 113 - pi |

    Trying for a solution;

    First of all, 113 needs to be one step more (114). If we take pi away, we get a negative number which is a sign that we need to increase 355. Do this until we get positive numbers again (when we look at the new error at the new fraction), then we increase 355 with 1 (and so on). Everytime we change, we also look if abs(newerror) < abs(old error)

    code:

    Code:
    function [t, n, err] = fracpi(t0, n0)
     
    e0 = t0/n0-pi;
     
    t = t0;
    n = n0;
     
    while (1)
        frac = t/n;
        if abs(frac-pi) < abs(e0), break, end
        if frac-pi < 0
            t=t+1;
        else
            n=n+1;
        end
    end
     
    err = frac-pi;
    end
    I thought this would be right. But Matlab tells its wrong.
    How does Matlab tell you this is wrong (it looks OK in principle)? You might try:

    Code:
    function [t, n, err] = fracpi(t0, n0)
     
      e0 = t0/n0-pi;
     
      t = t0;
      n = n0;
     
      while (1)
          frac = t/n;
          err=frac-pi;
          if abs(err) < abs(e0)
             return
          end
          if err < 0
              t=t+1;
          else
              n=n+1;
          end
      end
     
    end

    CB
    Last edited by CaptainBlack; February 8th 2011 at 01:05 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. loops in matlab, plotting matrix elements
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 10th 2011, 09:50 AM
  2. matlab for loops?
    Posted in the Math Software Forum
    Replies: 5
    Last Post: February 28th 2010, 09:49 PM
  3. [SOLVED] Matlab plotting help:peicewise with loops
    Posted in the Math Software Forum
    Replies: 4
    Last Post: October 20th 2009, 09:19 PM
  4. While loops
    Posted in the Math Software Forum
    Replies: 3
    Last Post: May 11th 2009, 06:54 PM
  5. While Loops! Help me please!!!!
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: October 8th 2007, 01:25 PM

Search Tags


/mathhelpforum @mathhelpforum