Matlab code for Engineering Physics not working

CODE
%Assigning values of required constants
k=1/(4*pi*8.542e-12) ;
z=linspace(-0.5,0.5) ;
E=zeros(size(z));
Q=1e-10;
%Creating a matrix containing position vectors for the 100 points on the z-axis
rz=linspace(-0.5,0.5) ;
rx=zeros(size(rz)) ;
ry=zeros(size(rz)) ;
r=[rx ; ry ; rz] ;
%Assigning variables to the position vectors
syms x [100 1]
for i=1 : 100
x(i)=transpose(r(: , i)) ;
end
%Creating a matrix containing position vectors for the 100 points on the y-axis
r1y=linspace(-0.5,0.5) ;
r1z=zeros(size(r1y)) ;
r1x=zeros(size(r1y)) ;
r1=[r1x ; r1y ; r1z] ;
%Assigning variables to the position vectors
syms x1 [100 1]
for i=1 : 100
x1(i)=transpose(r1(: , i)) ;
end
%Calculating distance between the points on z-axis and y-axis
for m=1 : 100
xi(m)=norm(x(m) - x1(m)) ;
end
E=zeros(z) ;
for m=1:100
E(m)=k*Q / abs(xi(m)) ^ 2 ;
end
%Analytical calculation and plotting graph of the solution
cons=1/(2*pi*8.542e-12)*1e-10 ;
y(1,1:100) = cons./abs(z) ;
plot(z,y,"red") ;
%Plotting graph of the solution obtained through code
hold on ;
box on ;
grid on ;
plot(z,E,"*") ;
xlabel('Distance h') ;
ylabel('Magnitude of E') ;
ERROR
Unable to perform assignment because the left and right sides have a different number of elements in assigning position vectors part.
THIS IS MY CODE PLEASE SUGGEST POSSIBLE CORRECTIONS

2 Comments

What errors are you getting? What is the specific issue? Please indicate exactly what you need help with.
Alan Weiss
MATLAB mathematical toolbox documentation
Extending Alan's comment - Your syntax is wrong in many places, not sure if that's because you have copy-pasted your code. Correct it and then mention what problems you are facing.

Sign in to comment.

 Accepted Answer

rz=linspace(-0.5,0.5) ;
rx=zeros(size(rz)) ;
ry=zeros(size(rz)) ;
r=[rx ; ry ; rz] ;
%Assigning variables to the position vectors
syms x [100 1]
size(r)
ans = 1×2
3 100
for i=1 : 100
x(i)=transpose(r(: , i)) ;
end
Unable to perform assignment because the left and right sides have a different number of elements.

Error in sym/privsubsasgn (line 1229)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);

Error in sym/subsasgn (line 1060)
C = privsubsasgn(L,R,inds{:});
So r is 3 x 100. You loop through by columns, extracting a 3 x 1 vector from it. You transpose that 3 x 1 vector to get a 1 x 3 vector. That is your right hand side. Then you try to assign that 1 x 3 vector to the scalar output location x(i) .
In MATLAB, it is not possible to define a numeric collection with () style indexing such that by using a single subscript, you get the content of the collection. It is possible to do that using {} style indexing.

2 Comments

%Assigning values of required constants
k=1/(4*pi*8.542e-12) ;
z=linspace(-0.5,0.5) ;
E=zeros(size(z));
Q=1e-10;
%Creating a matrix containing position vectors for the 100 points on the z-axis
rz=linspace(-0.5,0.5) ;
rx=zeros(size(rz)) ;
ry=zeros(size(rz)) ;
r=[rx ; ry ; rz] ;
%Assigning variables to the position vectors
for i=1 : 100
x{i}=transpose(r(: , i)) ;
end
%Creating a matrix containing position vectors for the 100 points on the y-axis
r1y=linspace(-0.5,0.5) ;
r1z=zeros(size(r1y)) ;
r1x=zeros(size(r1y)) ;
r1=[r1x ; r1y ; r1z] ;
%Assigning variables to the position vectors
for i=1 : 100
x1{i}=transpose(r1(: , i)) ;
end
%Calculating distance between the points on z-axis and y-axis
for m=1 : 100
xi(m)=norm(x{m} - x1{m}) ;
end
E=zeros(size(z)) ;
for m=1:100
E(m)=k*Q / abs(xi(m)) ^ 2 ;
end
%Analytical calculation and plotting graph of the solution
cons=1/(2*pi*8.542e-12)*1e-10 ;
y(1,1:100) = cons./abs(z) ;
plot(z,y,"red") ;
%Plotting graph of the solution obtained through code
hold on ;
box on ;
grid on ;
plot(z,E,"*") ;
xlabel('Distance h') ;
ylabel('Magnitude of E') ;
Oh, so that's the error. I understood now. Thanks for your explaination.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!