Help in Matlab Laplace Equation -
i have tried implement laplace equation in matlab code sequence shown below. created laplaceexplicit.m , used function numgrid in same. however, shows error "input variable n undefined". should done? code below-
function [x,y,t]= laplaceexplicit(n,m,dx,dy) echo off; numgrid(n,m); r = 5.0; t = r*ones(n+1,m+1); % t(i,j) = 1 includes boundary conditions x = [0:dx:n*dx];y=[0:dy:m*dy]; % x , y vectors = 1:n % boundary conditions @ j = m+1 , j = 1 6 t(i,m+1) = t(i,m+1)+ r*x(i)*(1-x(i)); t(i,1) = t(i,1) + r*x(i)*(x(i)-1); end; tn = t; % tn = new iteration solution err = tn-t; % parameters in solution beta = dx/dy; denom = 2*(1+beta^2); % iterative procedure epsilon = 1e-5; % tolerance convergence imax = 1000; % maximum number of iterations allowed k = 1; % initial index value iteration % calculation loop while k<= imax = 2:n j = 2:m tn(i,j)=(t(i-1,j)+t(i+1,j)+beta^2*(t(i,j-1)+t(i,j+1)))/denom; err(i,j) = abs(tn(i,j)-t(i,j)); end; end; t = tn; k = k + 1; errmax = max(max(err)); if errmax < epsilon [x,y] = meshgrid(x,y); figure(2);contour(x,y,t',20);xlabel('x');ylabel('y'); title('laplace equation solution - dirichlet boundary conditions explicit'); figure(3);surfc(x,y,t');xlabel('x');ylabel('y');zlabel('t(x,y)'); title('laplace equation solution - dirichlet boundary conditions explicit'); fprintf('convergence achieved after %i iterations.\n',k); fprintf('see following figures:\n'); fprintf('==========================\n'); fprintf('figure 1 - sketch of computational grid \n'); fprintf('figure 2 - contour plot of temperature \n'); fprintf('figure 3 - surface plot of temperature \n'); return end; end; fprintf('\n no convergence after %i iterations.',k);
matlab go through standard look-up procedure work out represents. first, local variable? if not, function or script (command)? requires looking in prescribed set of places. simple version is: first in current directory, in directories specified matlab path (in order).
hence, if write square_table.m , save in c:\work\moe\matlab directory needs either current working directory on on matlab path. otherwise error ("undefined function or variable").
Comments
Post a Comment