Prev: vertcat error
Next: Organizing data in Matlab
From: alfann on 22 Feb 2010 21:46 Hi there I want to run this code into Matlab program I tried many times but there is no way to run it correctly, could you please help me? clc clear all f1= input('The value of u(x,0) = '); f2= input('The value of u(x,b) = '); a= input('The (a) value of [0,a] = '); b= input('The (b) value of [0,b] = '); h= input('The step size = '); tol= input('The tolerance = '); max1= input('The maximum number of iterations = '); function U=dirich(f1,f2,a,b,h,tol,max1); n=fix(a/h)+1; m=fix(b/h)+1; ave=(a*(feval(f1,0)+feval(f2,0)))/(2*a); U=ave*ones(n,m); %Boundary Conditions U(1,1:m)=feval(f3,0:h:(m-1)*h)'; U(n,1:m)=feval(f4,0:h:(m-1)*h)'; U(1:n,1)=feval(f1,0:h:(n-1)*h)'; U(1:n,m)=feval(f2,0:h:(n-1)*h)'; U(1,1)=(U(1,2)+U(2,1))/2; U(1,m)=(U(1,m-1)+U(2,m))/2; U(n,1)=(U(n-1,1)+U(n,2))/2; U(n,m)=(U(n-1,m)+U(n,m-1))/2; %SOR parameter w=4/(2+sqrt(4-(cos(pi/(n-1))+cos(pi/(m-1)))^2)); %Refine approximations and sweep operator throughout the grid err=1; cnt=0; while((err>tol)&(cnt<=max1)) err=0; for j=2:m-1 for i=2:n-1 relx=w*(U(i,j+1)+U(i,j-1)+U(i+1,j)+U(i-1,j)-4*U(i,j))/4; U(i,j)=U(i,j)+relx; if (err<=abs(relx)) err=abs(relx); end end end cnt=cnt+1; end U=flipud(U')
From: Steven Lord on 23 Feb 2010 09:52 "alfann" <alfann.net(a)hotmail.com> wrote in message news:1833936556.254428.1266929232832.JavaMail.root(a)gallium.mathforum.org... > Hi there > I want to run this code into Matlab program > I tried many times but there is no way to run it correctly, could you > please help me? It appears syntactically legal, assuming that you forgot to copy and paste the function declaration line ("function foo", for example) at the beginning of your code. If you did, there's no need to call CLEAR ALL -- the fact that functions operate in their own workspaces takes care of that. You might want to actually call your subfunction inside the main function, though -- right now all it does is ask the user to input values for several variables, then the function ends. Put "U=dirich(f1,f2,a,b,h,tol,max1);" (without the quotes) at the end of the main function, just before the function declaration line (function U=dirich(f1,f2,a,b,h,tol,max1);) that ends the main function. If you didn't have a function declaration line at the top of this function then this is not syntactically legal as you cannot define a function inside of a script file, only inside a function file. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Jan Simon on 23 Feb 2010 10:01 Dear Alfann! > I want to run this code into Matlab program > I tried many times but there is no way to run it correctly, could you please help me? "No way to run it correctly" does not explain your problems with any details. Please post exactly: What do you want, what do you get, what error messages appear ? Kind regards, Jan
|
Pages: 1 Prev: vertcat error Next: Organizing data in Matlab |