N=10;h=(1-0)/(1+N);x=0:h:1;y=0:h:1;A=diag(-4*ones(1,N))+diag(ones(1,N-1),1)+diag(ones(1,N-1),-1);M=zeros(N^2,N^2);for i=1:NM((i-1)*N+1:i*N,(i-1)*N+1:i*N)=A;endfor i=1:N-1M((i-1)*N+1:i*N,(i)*N+1:(i+1)*N)=eye(N);endfor i=1:N-1M((i)*N+1:(i+1)*N,(i-1)*N+1:i*N)=eye(N);end[xx,yy] = meshgrid(x,y);f= -100.*xx .*yy;F=reshape(f,(N+2)^2,1);solutions=zeros((N+2)^2,1);for i=1:N for j=1:Nsolutions((j-1)*(N+2)+2:(j-1)*(N+2)+N+1)=h^2*M((i-1)*(N+1)+1:(i-1)*N+N,(j-1)*(N+1)+1:(j-1)*N+N)\F((j-1)*(N+2)+2:(j-1)*(N+2)+N+1); endend
MATLAB: Plz help me correct the last loop…
super long complicated expressions
Best Answer