Search code examples
matlabfunction-handle

Plotting a function handle of two variables


I have the following code:

f1_p1 = @(xq1) interp1(x_j1,p1,xq1);
f2_p1 = @(xq2) interp1(x_j2,p1,xq2);
new_p1x1 = @(xq1,xq2) f1_p1(xq1).*f2_p1(xq2);

To plot f1_p1 and f2_p2 is easy, I do:

fplot(f1_p1, [30,70])

My question

How can I plot the second function (new_p1x1)? I have tried the same as before but it doesn't work....(fplot(new_p1x1, [30,70])) I get:

Error using @(xq1,xq2)f1_p1(xq1).*f2_p1(xq2)
Not enough input arguments.

Thanks for your help!!!


Solution

  • When using

    fplot(new_p1x1, [30,70])
    

    [30,70] is considered as 1x2-matrix and thus only as one argument, while new_p1x1 requires two. Thus you can call either

    new_p1x1(30, 70) # 30 is passed to f1_p1 and 70 to f2_p2
    

    or

    new_p1x1([30,70], [30,70]) # The matrix [30,70] is passed to both function.
    

    I cannot tell, which solution is more useful for you, it depends on what you want to to.

    However, it seems, fplot only accepts functions with one argument. So it seems, you have to use one of the 3D plotting functions.

    For 3D plotting, you can use e.g. surf.

    [x1,~] = fplot(f1_p1, [30,70]); % Returns a useful number of x-values for f1
    [x2,~] = fplot(f2_p1, [30,70]); % Returns a useful number of x-values for f2
    
    [X,Y] = meshgrid(x1,x2);
    
    surf(x1,x2,new_p1x1(X,Y)); % x1 (X) is passed to f1, x2 (Y) is passed to f2