How to resolve a too many input argument error from using Function
조회 수: 8 (최근 30일)
이전 댓글 표시
First time actually using a Function in a code. I have created 3 functions so far to solve the Navier Stokes Equation for incompressable. The first function I use (periodicCopy) is used to make the boundary conditions periodic. This works fine. I am having trouble with my diffusion Function as it keeps saying too many input arguements. Originally I had variables that for some reason were not being used. After editing the function and making sure that each of the variables had a value from the main code and were used properly in the function I seem to still get an error for input. I am basically taking the same variables that I have used for my advection function which does work and for some reason does not work on diffusion. Could anyone please explain what I am doing wrong in this situation!?
Main code:
clear
clc
close all
%Start with all given variables
%Number of grid points
N = 31;
%This is in both x and y direction
Nx = N;
Ny = N;
%Viscousity is 1e -1
mu = 1e-1;
% The Domain is 2pi
L=2*pi;
% The domain is the same in both x and y
Lx = L;
Ly = L;
dx = Lx/(Nx-1);
dy = Ly/(Ny-1);
% Create N equally spaced values between 0 and L in both directions
xlin = linspace(0,Lx,Nx);
ylin = linspace(0,Ly,Ny);
[x,y] = meshgrid(xlin,ylin);
x=x';
y=y';
%Set up the initial velocities u for x direction v for y direction
u = cos(x).*sin(y);
v = -sin(x).*cos(y);
%delta t value given
dt= 1e-2;
%We are going up to a time of 10 seconds
tmax = 10;
% Set initial time to be 0 and set a counter
t = 0;
counter=0;
nGhosts=1;
%expanding our values for velocity for ghost cells
u_ex = zeros(Nx+2*nGhosts,Ny+2*nGhosts);
v_ex = zeros(Nx+2*nGhosts,Ny+2*nGhosts);
innerXstart = nGhosts + 1;
innerXend = nGhosts + Nx;
innerYstart = nGhosts + 1;
innerYend = nGhosts + Ny;
%Populate the inner values (non ghost cells) that we initially had set to 0
%we populate u and v as they change based on the value of x and y
u_ex(innerXstart:innerXend,innerYstart:innerYend) = u;
v_ex(innerXstart:innerXend,innerYstart:innerYend) = v;
u_exp = periodicCopy(u_ex, nGhosts);
v_exp = periodicCopy(v_ex, nGhosts);
%%
pressure_ex = zeros(size(u_ex));
errortimevec=[];
errorvec=[];
%Create the time loop
while(t<tmax)
%This will add 1 time step for each pass the loop does and also counts
%the steps using counter
t = t + dt;
counter = counter +1;
[u_exp, v_exp] = advection(u_exp, v_exp,dx, dy,dt,nGhosts);
%Solving the diffusion equation for both u and v
[u_exp, v_exp] = diffusion(u_exp, v_exp, dy, dt, nGhosts);
% rho = 1.0;
% divOp= DivOp(u_ex, v_ex, dx, dy, nGhosts);
% rhs = rho/dt *divOp;
% contourf(divOp);colorbar;
end
This is the Function that gets the error:
function [u_Exp, v_Exp] = diffusion(u_expa,v_expa, dy, dt, nGhosts)
%The diffusion equation is consisting of several values
%This should take the u_ex and v_ex from the Advection equation as the
%input
u_EXP = zeros(33,33);
v_EXP = zeros(33,33);
mu = 1e-1;
for i = 2:32
for j = 2:32
%Take the diffusion equation for u_ex
u_EXP(i,j) = u_expa(i,j) + mu*dt*((u_expa(i+1,j) - 2*u_expa(i,j) + u_expa(i-1,j))/(dy.^2);
%Take the diffusion equation for v_ex
v_EXP(i,j) = v_expa(i,j) + mu*dt*((v_expa(i,j+1) - 2*v_expa(i,j) + v_expa(i,j-1))/(dy.^2);
end
end
%PeriodicBC.m we get the ghost cell values
u_Exp = PeriodicBC(u_EXP,nGhosts);
v_Exp = PeriodicBC(v_EXP,nGhosts);
%The output of these values are the new u_ex and v_ex in the main code. or
%U** and V** in class notes used for the pressure projection.
댓글 수: 4
Bob Thompson
2019년 4월 22일
편집: Bob Thompson
2019년 4월 22일
Could you post your other functions as well, please? I would like to test your code myself, but am unable to without them.
채택된 답변
Bob Thompson
2019년 4월 23일
Thank you for posting the other functions. When I attempted to run your code I encountered an error due to the naming convention of the function. Using v2018b I had to rename the function because 'diffusion' is an included function.
That being said, the function ran fine with that change, the only difference I can tell between what I did and what you have is that 'advection' was commented out. I would double check that all of your variables are coming out of advection as you are expecting. IMO the best way to do this is to place a debug marker on the diffusion line, to examine your variables before they get fed into the function.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!