0

Let's say I have a function

f = @(x,y) g(x,y)

g is not explicit. It should be calculated numerically after inputting x and y values.

How can I calculate and save all values of this function at points

[x,y] = meshgrid(0:0.1:1,0:pi/10:2*pi);

I call f[x,y], but it doesn't work.

Cape Code
  • 3,584
  • 3
  • 24
  • 45
bordart
  • 249
  • 3
  • 11
  • 1
    See [this similar question](http://stackoverflow.com/q/21804025/2586922). I think my answer to that question can be applied here – Luis Mendo Feb 17 '14 at 21:50

1 Answers1

1

You have two ways to do it. Your function g should work with matrices, or you should give the values one by one

function eval_on_mesh()

f1 = @(x,y) g1(x,y);
f2 = @(x,y) g2(x,y);

[x,y] = meshgrid(0:0.1:1,0:pi/10:2*pi);

%# If your function work with matrices, you can do it like here
figure, surf(f1(x,y));

%# If your function doesnt, you should give the values one by one
sol = zeros(size(x));
for i=1:size(x,1)
    for j=1:size(x,2)
        sol(i,j) = f2(x(i,j),y(i,j));
    end
end
figure, surf(sol);
end

function res = g1(x, y)
res = x.^2 + y.^2;
end

function res = g2(x, y)
res = x^2 + y^2;
end
sebas
  • 869
  • 7
  • 16
  • It depends on your function. For basic operations, usualy you can write a dot `.` in front of the operator if you want to do the operations point-wise. Without knowing your function I can not tell you anything else. – sebas Feb 17 '14 at 21:07
  • The function is a very long expression under integral2 and it seems that the expression under integral works with matrices but with integration it doesn't. Strange. – bordart Feb 17 '14 at 21:20