# There is a error in my code. I have attached the equaion and my code. Kindly correct the part where i went wrong.

1 view (last 30 days)
Swathi S on 29 Jan 2019
Answered: Mark Sherstan on 29 Jan 2019
CODE:
r1=sqrt((x+a1).^2+(y+a2).^2+(z.^2)) %a1,a2,x,y and z are defined
r2=sqrt((a1-x).^2+(y+a2).^2+(z.^2))
r3=sqrt((a1-x).^2+(y-a2).^2+(z.^2))
r4=sqrt((x+a1).^2+(y-a2).^2+(z.^2))
C4=-a1-x
C1=-C4
C3=-a1+x
C2=-C3
d1=y+a2
d2=d1
d3=y-a2
d4=d3
I=2
for i=1:1:4
B=(((-1)^i*d(i))/(r(i)*(r(i)+((-1)^(i+1)*C(i)))))-(C(i)/(r(i)*(r(i)+d(i))))
Bz=((u*I)/(4*pi))*B
end
plot(z,Bz)
ERROR:
Undefined function or variable 'C'.

Mark Sherstan on 29 Jan 2019
You never defined a "C" variable. You have C1, C2, etc... but not just "C". Additionally, you need to perform a summation in the loop (you are currently just redefining B each time) so you should adjust the end of your code as such:
...
B = 0;
for i=1:1:4
B = (((-1)^i*d(i))/(r(i)*(r(i)+((-1)^(i+1)*C(i)))))-(C(i)/(r(i)*(r(i)+d(i))))
B = B + B;
end
Bz=((u*I)/(4*pi))*B
plot(z,Bz)