Results 1 to 4 of 4

Math Help - need a m-file

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    24

    need a m-file

    Van der Waals’s equation of state for an imperfect gas is

    ( P + a / v ^ 2) ( v - b ) = R T

    where P is the pressure (atm), v is the molar volume (liters/mole), T is the absolute temperature (K), and a (liter
    2.atm/mol2), and b (liter/mol) are constants that depend on the particular gas. Find
    molar volume of carbon dioxide at 3 atm and 320 K. For carbon dioxide, you may use a=3.592

    and b=0.04267. (Hint: Use ideal gas law to make your initial guess.)

    a) Write a generic MATLAB m-file that uses Newton’s (Newton-Raphson) method to solve for x
    given the function f(x), initial guess x0, and error tolerance es. Make use of the attached
    pseudocode given for the fixed-point iteration method.
    b) Write a MATLAB m-file that uses the generic m-file you have developed in part (a) to solve for
    the molar volume of any gas, given the P, T, R, a, and b. Use a relative error tolerance of
    0.01% for convergence of molar volume.
    c) Run the m-file you have developed in part (b) along with the generic m-file of part (a) for the
    case of carbon dioxide given above. Make sure you display the result at each iteration.
    d) Plot error as a function of the iteration step. Do not forget to label the axes.
    e) MATLAB has a built-in function called fzero to find the real root of a single equation. A
    simple representation of its syntax is
    fzero(function, x0)
    where function is the name of the function being evaluated, and x0 is the initial guess.
    Solve the above problem using fzero. Comment on the result by comparing with the result
    of (c).

    How can i solve this; İ need m-file?
    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 chronicals View Post
    Van der Waals’s equation of state for an imperfect gas is

    ( P + a / v ^ 2) ( v - b ) = R T

    where P is the pressure (atm), v is the molar volume (liters/mole), T is the absolute temperature (K), and a (liter
    2.atm/mol2), and b (liter/mol) are constants that depend on the particular gas. Find
    molar volume of carbon dioxide at 3 atm and 320 K. For carbon dioxide, you may use a=3.592

    and b=0.04267. (Hint: Use ideal gas law to make your initial guess.)

    a) Write a generic MATLAB m-file that uses Newton’s (Newton-Raphson) method to solve for x
    given the function f(x), initial guess x0, and error tolerance es. Make use of the attached
    pseudocode given for the fixed-point iteration method.
    b) Write a MATLAB m-file that uses the generic m-file you have developed in part (a) to solve for
    the molar volume of any gas, given the P, T, R, a, and b. Use a relative error tolerance of
    0.01% for convergence of molar volume.
    c) Run the m-file you have developed in part (b) along with the generic m-file of part (a) for the
    case of carbon dioxide given above. Make sure you display the result at each iteration.
    d) Plot error as a function of the iteration step. Do not forget to label the axes.
    e) MATLAB has a built-in function called fzero to find the real root of a single equation. A
    simple representation of its syntax is
    fzero(function, x0)
    where function is the name of the function being evaluated, and x0 is the initial guess.
    Solve the above problem using fzero. Comment on the result by comparing with the result
    of (c).

    How can i solve this; İ need m-file?
    No, it clearly says you are to write an m-file to do this using the code you should have been provided with for fixed point iteration as a model for the Newton Raphson function.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    24
    This is my m-file,can you check it,please? I can't see the plot?Why? I use [root]=newtoniandream( ' y ', 1e-3)

    function [f, derf ]=y(v)
    a=3.592; b=0.04267; P=3; T=320; R=0.082;
    f= (P+a/v^2)*(v-b)-R*T;
    derf=P-a/v^2+2*a*b/v^3;


    function [root]=newtoniandream(v0,tol)
    v=v0;
    n=0;
    err=3;
    fprintf(
    'iteration x percent relative error\n')
    while err>tol
    n=n+1;
    [f, derf ]=feval(
    'y',v);
    vnew=v-f/derf;
    err(n)=(abs((vnew-v)/vnew))*100;
    fprintf(
    '%2d %f %f\n',n,vnew,err(n))
    v=vnew;
    plot(n,err)
    hold
    on
    end
    root=vnew
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by chronicals View Post
    This is my m-file,can you check it,please? I can't see the plot?Why? I use [root]=newtoniandream( ' y ', 1e-3)
    I'm surprised that this runs at all (or rather does not go into a infinite loop)

    Try:

    Code:
    function [root,err]=newtoniandream(v0,tol)
      v=v0;
      n=0;
      err0=3;
      fprintf('iteration x percent relative error\n')
      while err0>tol
        n=n+1;
        [f, derf ]=feval('y',v);
        vnew=v-f/derf;
        err(n)=(abs((vnew-v)/vnew))*100;
        err0=err(n);
        fprintf('%2d %f %f\n',n,vnew,err(n))
        v=vnew;
       end
       root=vnew;
       plot(err);
    with:

    [root,err]=newtoniandream( 8, 1e-3)

    as the calling command, then use (if you want):

    plot(err)

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. how to make the PDF file smaller
    Posted in the LaTeX Help Forum
    Replies: 8
    Last Post: July 28th 2010, 12:24 PM
  2. m file failure?
    Posted in the Math Software Forum
    Replies: 18
    Last Post: April 26th 2010, 05:15 AM
  3. Differentiation m-file help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 17th 2010, 10:20 PM
  4. Mat lab - Referencing M-file in another M-file
    Posted in the Math Software Forum
    Replies: 5
    Last Post: December 16th 2009, 03:53 AM
  5. Matlab M file
    Posted in the Math Software Forum
    Replies: 4
    Last Post: April 18th 2009, 04:34 AM

Search Tags


/mathhelpforum @mathhelpforum