Clear Filters
Clear Filters

How to resolve a too many input argument error from using Function

6 views (last 30 days)
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.
Capture.PNG
  4 Comments
Bob Thompson
Bob Thompson on 22 Apr 2019
Edited: Bob Thompson on 22 Apr 2019
Could you post your other functions as well, please? I would like to test your code myself, but am unable to without them.
James McGinley
James McGinley on 22 Apr 2019
function matrixOut = periodicCopy(matrixIn,nGhosts)
% Note: The very corner ghost cells are never actually used
matrixOut = matrixIn;
% Periodic copy into left ghosts
% Remember, must exclude the ghosts themselves on the right boundary
matrixOut(:,1) = matrixIn(:,end-1);
% Periodic copy into right ghosts
matrixOut(:,end) = matrixIn(:,nGhosts+1);
% Periodic copy into top ghosts
matrixOut(1,:) = matrixIn(end-1,:);
% Periodic copy into bottom ghosts
matrixOut(end,:) = matrixIn(nGhosts+1,:);
I believe I would need to comment the periodicCopy in the diffusion equation out

Sign in to comment.

Accepted Answer

Bob Thompson
Bob Thompson on 23 Apr 2019
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.
  1 Comment
James McGinley
James McGinley on 23 Apr 2019
@Bob Nbob. Thank you for your answer, I did not think about the possibility of Matlab already having a function named Diffusion. After changing the name of my function the code works just fine! Thank you very much for taking the time to help me!

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!