0

Is there a way in matlab to restrict variables in a function

For example i have a function

function S0 = S0Func(obj, c, delta, xT, sigma)
beta = obj.betaFunc(sigma);
xb = obj.xbFunc(c, delta, sigma);
S0 = (1-obj.tau).*(obj.x0./(obj.r-obj.mu)-c./obj.r-(xb./(obj.r-obj.mu)-c./obj.r).*((obj.x0./xb).^beta)-((delta-1).*c./obj.r).*((obj.x0./xT).^beta-((obj.x0./xb).^beta)));
end

where I would like to have the restrictions (obj is an object of a class)

0<xb<xT<1
0<c
1<delta

What I would like to do is to draw a 3d graph of the following with the restrictions mentioned above

S0Func(2.7, 1, 1, 0.3)-S0Func(c,delta,xT,0.2)<0;

EDIT I have tried using the isosurface

optimalStraightCoupon = fminbnd(@(c) -(S0Function(c,1,1)+D0Function(c,1,1)), 0, 4);
[xT, delta, c] = meshgrid(0.8:.01:1, 1:.1:3, 0:.1:4);
values = S0Function(optimalStraightCoupon,1, 1)- S0Function(c,delta, xT);            
patch(isosurface(xT, c, delta, values, 1), 'FaceColor', 'red');
view(3);

I get some output, but it is not correct as the restriction on xT is violated.

Any help is appreciated. Thank you.

Bastaix
  • 835
  • 3
  • 12
  • 23
  • What would you like to happen if the user calls your function with arguments that don't satisfy your restrictions? Do you want an error to be thrown? – Chris Taylor Apr 20 '12 at 08:35
  • @ChrisTaylor I have updated the question with further information. – Bastaix Apr 20 '12 at 08:38

3 Answers3

0

It's a little unclear what you are trying to achieve. I expect that you know that you can write statements such as (pseudocode):

if c>=0 exit

which, in a sense, restricts your function to operate only on values which meet the defined constraints.

The foregoing is so simple that I am sure that I have misunderstood your question.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

No, it appears you wish MORE than a just 3-d graph. It appears you wish to see this function as a FUNCTION of three variables: c, delta, xT. So a 4-d graph. In that event, you will need to simply evaluate the function over a 3-d mesh (using meshgrid or ndgrid to generate the points.)

Then use isosurface to visualize the result, as essentially a higher dimensional contour plot. Do several such plots, at different iso-levels.

  • I want a graph in three dimension of (c, delta, xT) which satisfies that the difference in S0 is less than zero as well as the restrictions are satisfied. – Bastaix Apr 20 '12 at 10:38
  • And, again, this is an isosurface!!!!!! Why not try it, rather than just telling us what code you want us to write for you. help isosurface –  Apr 20 '12 at 12:56
  • I have tried the isosurface. But I still do not know how I make sure that the restrictions is satisfied. I will edit the question with the isosurface attempt – Bastaix Apr 20 '12 at 13:10
0

I would just return NaN when your constraints are violated.

Just add following lines to your function (preferable before the calculation to save time)

if delta <= 1 || c <= 0 || ... % I assume you can write them yourself
   S0 = NaN;
   return
end

Plot will not draw NaNs.

Though as you input c,delta,xT it's the question why they are set to invalid values in the first place. You could save some checks and therefore time if you assure that beforehand.

bdecaf
  • 4,652
  • 23
  • 44