Calculating gradient of a matrix - too many outputs... Not sure why?

2 次查看(过去 30 天)
Hello,
I have calculated a result matrix using the integrating function on matlab, however when I try to calculate the gradient of the result matrix, it says I have too many outputs.
My code is as follows:
x = linspace(-1,1,40);
y = linspace(-2,2,40);
for ii = 1:numel(x);
for jj = 1:numel(y)
fun = @(x) x(ii) + y(jj)
V(ii,jj) = integral(fun, 0, 2());
end
end
[qx,qy] = -gradient(V);
I tried to set up a meshgrid first to do my calculation over x and y, however the integral matlab function couldn't handle a meshgrid.
Any idea how I could calculate the gradient of my resulting V function?
Note that I have replaced my actual function with a more simple function. I could not calculate the integral of my function directly and thus am resorting to numerical integration.
  2 个评论
Stephen23
Stephen23 2017-11-22
Please show the complete error message. This means all of the red text.
Mark Lepage
Mark Lepage 2017-11-22
编辑:Mark Lepage 2017-11-22
Hey Stephen, my apologies, I had an error in my above code (I have fixed it)
But here is the complete error message:
>> [qx,qy] = -gradient(V);
Error using -
Too many output arguments.

请先登录,再进行评论。

采纳的回答

Mark Lepage
Mark Lepage 2017-11-22
I can't believe I didn't notice this,
But the error stems from the negative sign I had in front of the gradient function.
I removed this, and everything works great.
x = linspace(-1,1,40);
y = linspace(-2,2,40);
for ii = 1:numel(x);
for jj = 1:numel(y)
fun = @(x) x(ii) + y(jj)
V(ii,jj) = integral(fun, 0, 2());
end
end
[qx,qy] = gradient(V);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by