Results 1 to 3 of 3

Math Help - Matlab; Defining a Function with various constants

  1. #1
    Super Member
    Joined
    Jun 2009
    From
    United States
    Posts
    676
    Thanks
    19

    Matlab; Defining a Function with various constants

    So when I create a function with arbitrary constants, for instance f(x)=\frac{a}{x}-\frac{b}{x^2}, how can I make multiple plots of this function using different values for the parameters 'a' and 'b' without having to make a separate file for each pair of (a,b).... My function file looks like this,

    function Veff = V(x)
    a = 1
    b = 1
    Veff = a*x.^(-1) - b*x.^(-2)

    I also have another file that creates the plot when I run it, although the code I'm using for the x and y labels does not seem to be working. The graph shows up without the labels.



    xlabel('Radial Distance');
    ylabel('Effective Potential');
    y = V(x);
    plot(x,y)
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Apr 2012
    From
    Erewhon
    Posts
    188
    Thanks
    126

    Re: Matlab; Defining a Function with various constants

    Quote Originally Posted by adkinsjr View Post
    So when I create a function with arbitrary constants, for instance f(x)=\frac{a}{x}-\frac{b}{x^2}, how can I make multiple plots of this function using different values for the parameters 'a' and 'b' without having to make a separate file for each pair of (a,b).... My function file looks like this,

    function Veff = V(x)
    a = 1
    b = 1
    Veff = a*x.^(-1) - b*x.^(-2)
    define a and b at the console or in the outer script, then

    Code:
    function Veff = V(x) 
       global a
       global b
       Veff = a*x.^(-1) - b*x.^(-2);
    should work, otherwise add them as additional arguments.

    .
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Apr 2012
    From
    Erewhon
    Posts
    188
    Thanks
    126

    Re: Matlab; Defining a Function with various constants

    Quote Originally Posted by adkinsjr View Post
    I also have another file that creates the plot when I run it, although the code I'm using for the x and y labels does not seem to be working. The graph shows up without the labels.



    xlabel('Radial Distance');
    ylabel('Effective Potential');
    y = V(x);
    plot(x,y)
    Do the plot before adding the labels.

    .
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. defining of the function (Mathematica)
    Posted in the Math Software Forum
    Replies: 0
    Last Post: October 24th 2011, 02:36 AM
  2. Defining a function in mathematica
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 22nd 2010, 08:36 AM
  3. matlab - defining boundaries
    Posted in the Math Software Forum
    Replies: 2
    Last Post: December 15th 2009, 04:12 AM
  4. Defining function
    Posted in the Calculus Forum
    Replies: 1
    Last Post: January 9th 2009, 02:36 PM
  5. problem with defining a function
    Posted in the Advanced Math Topics Forum
    Replies: 3
    Last Post: August 29th 2005, 07:23 AM

Search Tags


/mathhelpforum @mathhelpforum