How do i write the function for 3D graph which include summation?
Show older comments
I would like to create a 3D graph for function z, where z takes the form of:
z=3x+2y+sum(k[i]*sqrt((y-a[i])^2+(x-b[i])^2)
Suppose i=1:3,
the values for k[i],a[i],b[i] are known.
I have defined
k=[c1 c2 c3];
a=[c4 c5 c6];
b=[c7 c8 c9];
where c is constant.
The function i can think of is:
z=3*x+2*y+sum(k.*sqrt((y-a).^2+(x-b).^2)
but it seems to be wrong as i keep getting the error message of "Matrix dimensions must agree."
What would be the correct way for constructing above function?
Answers (1)
Walter Roberson
on 25 Jul 2012
[x, y] = ndgrid( xvalues, yvalues );
z = 3 * x(:) + 2 * y(:) + sqrt( bsxfun(@minus, y(:), a).^2 + bsxfun(@minus, x(:), b).^2 ) * k;
Reshape to size(x) if you want.
Note that the "* k" is intended as matrix multiplication, and thus implicitly includes doing the summation.
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!