ERROR: Sample points vector corresponding to grid dimension 1 must contain 40 elements.

8 visualizzazioni (ultimi 30 giorni)
Hello there. I am trying to write a code to integrate over coulombs law to find the Electric Potential of a continuous line charge distribution for an assignment, of which I also need a graph of. I wrote the code, and I think it makes sense, but I can't seem to get it to graph. Here is the code:
  1. %Date
  2. %Assignment
  3. % This is a code to calculate the Electric Potential of a continuous line charge as a function of
  4. % charge and distance by integrating Coulomb's Law.
  5. close all;
  6. clear;
  7. % Here I am setting up variables that I will use later. I set up [x,y,z] as
  8. % a meshgrid so I could get a nice graphic. i, j, and k arethe indeces of my
  9. % array Vector space and correspond to x, y, and z. kprime is the source
  10. % point of my continuous charge distribution, to be subtracted from z. The
  11. % rest are constants for the Potential Calculation.
  12. [x,y,z] = meshgrid(-1:0.05:1,-1:0.05:1,-1:0.05:1)
  13. i = 1;
  14. j = 1;
  15. k = 10;
  16. kprime = 1;
  17. lambda = 2*(10^(-12));
  18. Eo = 8.85*(10^(-12));
  19. kc = (1/(4*pi*Eo));
  20. %VectorSpace(x,y,z) = zeros(40);
  21. % I figured I would use for loops as an integration technique, and start
  22. % out with just calculating the space which I called VectorSpace. Using the
  23. % indeces i, j, k and kprime, I then set x, y, z, and zprime to the spaces
  24. % I want the vector to be displayed over (From -1 to 1 with volume slices
  25. % of 0.05) I did this because I can't start the for loop with a negative
  26. % number.
  27. for i = 1:1:40
  28. for j = 1:1:40
  29. for k = 1:1:40
  30. for kprime = 10:1:20
  31. x = (i*0.05) - 1;
  32. y = (j*0.05) - 1;
  33. z = (k*0.05) - 1;
  34. zprime = (kprime*0.05)/2;
  35. VectorSpace(i,j,k) = 1/sqrt(x^2 + y^2 + (z - zprime)^2);
  36. kprime = kprime + 1;
  37. end
  38. k = k + 1;
  39. end
  40. j = j + 1;
  41. end
  42. i = i + 1;
  43. end
  44. % Then I take the VectorSpace and Multiply it by Coulombs constant (Defined
  45. % above) so it contains the proper values for the Electric Potential.
  46. PotV = -(kc)*(lambda)*VectorSpace;
  47. % Here I try to plot the results using the slice function to get a 3-D
  48. % Volumetric Picture of the Potential.
  49. figure(1);
  50. %plot3(PotV);
  51. slice(x,y,z,PotV,[-1:0.05:1],[-1:0.05:1],[-1:0.05:1]);
  52. % And below are some modifications to the figure
  53. shading interp;
  54. colormap hsv;
  55. xlabel('x');
  56. ylabel('y');
  57. zlabel('z');
  58. title('Electric Potential of line charge on Z-Axis');
  59. axis square;
  60. colorbar;
  61. rotate3d on;
  62. % Add another graph here?
  63. pause;
There are some comments that are just temporary. Anyway, I was thinking of getting a 3d picture using the slice function, but I keep getting this error:
Error using griddedInterpolant
Sample points vector corresponding to grid dimension 1 must contain 40 elements.
Error in interp3 (line 134)
F = griddedInterpolant({X, Y, Z}, V, method,extrap);
Error in slice (line 103)
vi = interp3(x,y,z,v,xi,yi,zi,method);
Error in Untitled8 (line 80)
slice(x,y,z,PotV,[-1:0.05:1],[-1:0.05:1],[-1:0.05:1]);
And I don't know what it exactly means. I tried changing around the dimensions of the slice, and the dimensions of the meshgrid. But all the meshgrid, slice, and PotV and VectorSpace I think all have 40 elements. I also tried graphing it using different functions, plot, plot3, etc, but for some reason I can't get them to work right.
If anyone could provide any insight as to how to fix it, or if someone knows a better way to graph my PotV, that would be greatly appreciated :)
Thank you!

Risposte (1)

Omer Babiker
Omer Babiker il 8 Feb 2021
I've been getting a similar error message. Before you can interpolate the grid and the vcalues for the original pionts must match up. DId you check if the array "v" in your case has the same number of elements in the 1st dimention as "x". The varables "x","y" and "z" represent where your to be interpolated values are located, and must therefore have the same number elements in each dimention as "v". Simply looking up in your workspace or using the "size" function should help identify the mismatch.
Hope this helps.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by