Error Using Gradient Function

조회 수: 6 (최근 30일)
Carl
Carl 2011년 10월 6일
I am attempting to write a program that will solve for the electric field and charge density (rho_v) at a point P given a function for voltage. This is a very straight forward problem on paper but I'm having some issues translating this to code. E is found with E = -(del)V and rho_v found with Gauss' Law. To start I shift the coordinates given for P from Cartesian to spherical since I find it makes Gauss' Law easier later. I believe I am using the gradient function correctly as it is described on the mathworks description page. But every time I run the program I get the following error:
Error in ==> gradient at 49
[msg,f,ndim,loc,rflag] = parse_inputs(f,varargin);
??? Output argument "varargout{2}" (and maybe others) not assigned during call to "C:\Program
Files\MATLAB\R2011a Student\toolbox\matlab\datafun\gradient.m>gradient".
Error in ==> Test at 19
[V_r,V_theta,V_phi] = gradient(V);
CODE
x = -3;
y = 1;
z = 2;
eta_0 = 8.85*10^-12;
r = sqrt(x^2 + y^2 + z^2);
theta = atan(sqrt(x^2 + y^2)/z);
phi = atan(y/x);
x = r*cos(theta);
y = r*sin(theta)*cos(phi);
z = r*cos(theta);
V = 5*x.^3*y.^2*z;
[V_r,V_theta,V_phi] = gradient(V);
E = [V_r,V_theta,V_phi];
rho_i = @(r,theta,phi)r.^2*sin(theta);
rho_f = triplequad(rho_i,0,r,0,pi,0,2*pi);
rho_v = (eta_0*E)/rho_f;
Any assistance solving this error would be greatly appreciated.

답변 (1개)

Jan
Jan 2011년 10월 6일
You "believe" that you are running gradient correctly, but did you check this? If you use the debugger either by setting a breakpoint or by dbtop if error you can find out very fast, that V is scalar, to be exact with the value 720. And you cannot calculate a gradient with 3 dimensions from a scalar.
Do you want the partial derivatives of "5*x.^3*y.^2*z"? This can be computed manually.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by