ERROR: Sample points vector corresponding to grid dimension 1 must contain 40 elements.
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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:
- %Date
- %Assignment
- % This is a code to calculate the Electric Potential of a continuous line charge as a function of
- % charge and distance by integrating Coulomb's Law.
- close all;
- clear;
- % Here I am setting up variables that I will use later. I set up [x,y,z] as
- % a meshgrid so I could get a nice graphic. i, j, and k arethe indeces of my
- % array Vector space and correspond to x, y, and z. kprime is the source
- % point of my continuous charge distribution, to be subtracted from z. The
- % rest are constants for the Potential Calculation.
- [x,y,z] = meshgrid(-1:0.05:1,-1:0.05:1,-1:0.05:1)
- i = 1;
- j = 1;
- k = 10;
- kprime = 1;
- lambda = 2*(10^(-12));
- Eo = 8.85*(10^(-12));
- kc = (1/(4*pi*Eo));
- %VectorSpace(x,y,z) = zeros(40);
- % I figured I would use for loops as an integration technique, and start
- % out with just calculating the space which I called VectorSpace. Using the
- % indeces i, j, k and kprime, I then set x, y, z, and zprime to the spaces
- % I want the vector to be displayed over (From -1 to 1 with volume slices
- % of 0.05) I did this because I can't start the for loop with a negative
- % number.
- for i = 1:1:40
- for j = 1:1:40
- for k = 1:1:40
- for kprime = 10:1:20
- x = (i*0.05) - 1;
- y = (j*0.05) - 1;
- z = (k*0.05) - 1;
- zprime = (kprime*0.05)/2;
- VectorSpace(i,j,k) = 1/sqrt(x^2 + y^2 + (z - zprime)^2);
- kprime = kprime + 1;
- end
- k = k + 1;
- end
- j = j + 1;
- end
- i = i + 1;
- end
- % Then I take the VectorSpace and Multiply it by Coulombs constant (Defined
- % above) so it contains the proper values for the Electric Potential.
- PotV = -(kc)*(lambda)*VectorSpace;
- % Here I try to plot the results using the slice function to get a 3-D
- % Volumetric Picture of the Potential.
- figure(1);
- %plot3(PotV);
- slice(x,y,z,PotV,[-1:0.05:1],[-1:0.05:1],[-1:0.05:1]);
- % And below are some modifications to the figure
- shading interp;
- colormap hsv;
- xlabel('x');
- ylabel('y');
- zlabel('z');
- title('Electric Potential of line charge on Z-Axis');
- axis square;
- colorbar;
- rotate3d on;
- % Add another graph here?
- 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!
0 Commenti
Risposte (1)
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!