Example Using Functions
Once the coding skills are well astablished next comes using functions, as a result the some process are needed several times while running the code, having the same set of commands read by matlab over and over agins slows down your comuter and makes the code more difficult to read.
The user for some cases has to relate the function to different situations as an example it might be called at a certain time step and some times at certain domain points.
once the functions have been written and the input and output are specfied, next comes saving them or copying them to the directory where they will be called from, as a default matlab aquires the called function from the directory that the M file is saved on. It is advisable to gather the m file and functions in a specfied folder and run matlab from that folder.
The user for some cases has to relate the function to different situations as an example it might be called at a certain time step and some times at certain domain points.
once the functions have been written and the input and output are specfied, next comes saving them or copying them to the directory where they will be called from, as a default matlab aquires the called function from the directory that the M file is saved on. It is advisable to gather the m file and functions in a specfied folder and run matlab from that folder.
A Function Example
Copy the two codes on two seperate files and save them in one folder, when the main one is run it should call the second one. You can bulid a CFD code based on the principle of using several functions. These can be called and save you lots of time especially for the purpuse of reducing the number of read lines by the complier, the researcher would realize at advanced stages of his project that time becomes an important factor when running calculatios.
The Main Code
The following code has the input paramters assigned values and at the end a function is called. It still needs to be modfied to get it in the proper form:
clc
clear
LX=1;
LY=1;
N=4;
M=4;
dx=LX/M
dy=LY/N
der2Uder2X(M,N,dx,dy)
clear
LX=1;
LY=1;
N=4;
M=4;
dx=LX/M
dy=LY/N
der2Uder2X(M,N,dx,dy)
The function Code
The function is written and the algebric operations are defined inside it. Need to mention here that I have related the u velocity component to a parabolic function, this is a case that can be applied for an inital condition for a simulation, the researcher can also use some random generatour for the initially case.
function der2Uder2X(M,N,dx,dy)
for t=1:3
for i=1:M;
for j=1:N;
x(i,j,t)=(i)*dx-dx
x(i+1,j,t)=i*dx
x(i+2,j,t)=i*dx+dx
u(i+2,j,t)=(x(i+2,j,t))^2
u(i+1,j,t)=(x(i+1,j,t))^2
u(i,j,t)=(x(i,j,t))^2
d2udx2(i,j,t)=(u(i+2,j,t)-2*u(i+1,j,t)+u(i,j,t))/(dx)^2
end
end
end
surf(d2udx2(:,:,2))
set(gca,'XLim',[0 5],'YLim',[0 5])
title('Second Order Scheme')
xlabel('x')
ylabel('y')
zlabel('d2udx2')
for t=1:3
for i=1:M;
for j=1:N;
x(i,j,t)=(i)*dx-dx
x(i+1,j,t)=i*dx
x(i+2,j,t)=i*dx+dx
u(i+2,j,t)=(x(i+2,j,t))^2
u(i+1,j,t)=(x(i+1,j,t))^2
u(i,j,t)=(x(i,j,t))^2
d2udx2(i,j,t)=(u(i+2,j,t)-2*u(i+1,j,t)+u(i,j,t))/(dx)^2
end
end
end
surf(d2udx2(:,:,2))
set(gca,'XLim',[0 5],'YLim',[0 5])
title('Second Order Scheme')
xlabel('x')
ylabel('y')
zlabel('d2udx2')
This is a cool link:
http://www.ws.binghamton.edu/fowler/fowler%20personal%20page/EE521_files/MATLAB%20Functions.pdf
http://www.ws.binghamton.edu/fowler/fowler%20personal%20page/EE521_files/MATLAB%20Functions.pdf
Example
function output=g(x,y)
a=x.*y;
output=a;
a=x.*y;
output=a;
Unless otherwise noted, all content on this site is @Copyright by Ahmed Al Makky 2012-2013 - http://cfd2012.com