# Matlab; Defining a Function with various constants

• Mar 24th 2014, 01:43 PM
Matlab; Defining a Function with various constants
So when I create a function with arbitrary constants, for instance $\displaystyle 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.

ylabel('Effective Potential');
y = V(x);
plot(x,y)
• Mar 27th 2014, 06:20 AM
zzephod
Re: Matlab; Defining a Function with various constants
Quote:

So when I create a function with arbitrary constants, for instance $\displaystyle 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);

.
• Mar 27th 2014, 06:36 AM
zzephod
Re: Matlab; Defining a Function with various constants
Quote: