Below is my code to solve the parallel plate capacitor problem.
clear allx=linspace(1,20,10);y=linspace(1,20,10);[X,Y]=meshgrid(x,y);C=delaunay(X(:),Y(:));xy=[X(:) Y(:)];Np=length(xy);S=MatGlob(xy,C);F=sparse(Np,1);V0=1;G=1e12;for k=1:Np;if x(k)==1S(k,k)=S(k,k)+G;F(k)=G*(-V0);if x(k)==20S(k,k)=S(k,k)+G;F(k)=G*(+V0);endV=S\F;figureplot(xy,V,'*-')title('potential(V)')xlabel('postition(m)')ylabel('postition(m)')hold onfunction S=MatGlob(xy,C)Np=length(xy);S=sparse(Np,Np);Epsr=3.5;Ne=size(C,1);for k=1:Ne N=C(k,:); x1=xy(N(1),1); x2=xy(N(2),1); x3=xy(N(3),1); y1=xy(N(1),2); y2=xy(N(2),2); y3=xy(N(3),2); abc1=[1 x1 y1;1 x2 y2;1 x3 y3]\[1;0;0]; abc2=[1 x1 y1;1 x2 y2;1 x3 y3]\[0;1;0]; abc3=[1 x1 y1;1 x2 y2;1 x3 y3]\[0;0;1]; Grad=[abc1(2) abc2(2) abc3(2); abc1(3) abc2(3) abc3(3)]; Se=Epsr*(Grad'*Grad)*abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2; S(N,N)=S(N,N)+Se;endend
Yet, Matlab annouce the error as follows: "Function definition not supported in this context. Create functions in code file" Would you please help me to fix this error? Thank you in advance.
Best Answer