How can I add vector input to this MatLab function?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around modifying a MATLAB function to accept vector inputs instead of just scalar inputs. Participants explore the necessary changes to the function definition and the implications of these changes on the function's execution.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes an issue with a MATLAB function that only accepts scalar inputs and seeks help to modify it to accept a vector input.
  • Another participant provides a modified version of the function that successfully incorporates the vector input, suggesting that the original poster may have missed adding the vector in all necessary places.
  • There is a discussion about an error message related to insufficient input arguments, with participants trying to identify the source of the error based on line numbers and variable definitions.
  • One participant points out that a variable defined in the command window is not accessible in the nested function, leading to an error, and suggests defining it within the function instead.
  • Concerns are raised about unused variables in the function and the potential for confusion due to variable naming conflicts with MATLAB functions.
  • Participants clarify the difference between running a script and calling a function with parameters, which resolves the original poster's issue.

Areas of Agreement / Disagreement

Participants generally agree on the necessary modifications to the function to accept vector inputs, but there is some confusion regarding the execution of the function and the source of error messages. The discussion remains somewhat unresolved regarding the best practices for defining and using variables in MATLAB functions.

Contextual Notes

Limitations include the potential for variable naming conflicts and the need for careful management of variable scope within MATLAB functions. The discussion highlights the importance of understanding how to properly call functions with multiple inputs.

Who May Find This Useful

This discussion may be useful for MATLAB users looking to modify functions to handle vector inputs, as well as those troubleshooting similar issues related to function execution and variable scope.

member 428835
Hi pf

I have a question that I have not been able to find. The below function accepts outside scalar inputs but not vectors. The code is below.

Also, ## a, b, delta,FA,Hi,vHnew,iv## are all scalars defined outside the function. The vector I would like to add as an input is ##x##, which I've commented below what it could be. However, even if I add this to the function line and ##fa, fb##, and ##fc##, along with the ##fx## function at the end, I still receive "Error Not enough input arguments." Please help me so this function can operate on ##x## when it is defined outside the function.

Any help is greatly appreciated!

Code:
function c = mshvol(a, b, delta,FA,Hi,vHnew,iv)
   fa = f(a,FA,Hi,vHnew,iv);
   fb = f(b,FA,Hi,vHnew,iv);
while ( abs(b - a) > 2*delta )
   c = (b + a)/2;
   fc = f(c,FA,Hi,vHnew,iv);
    if sign(fc) ~= sign(fb)
      a = c; fa = fc;
    else
      b = c; fb = fc;
   end% end if
end

function fx = f(zbulk,FA,Hi,vHnew,iv)
dx=.001;
% x=0:dx:5;
y=25-x.^2;
diff = abs(x-zbulk);
[~, idx] = min(diff); %index of closest value
z=y(idx:end);
fx = zbulk*15+trapz(z)*dx-70; %% Volume balance
return;
 
Physics news on Phys.org
Using these test values for the variables:
Code:
delta = 1;
FA = 1;
Hi = 1;
vHnew = 1;
iv = 1;
x = 1:10;
a = 10;
b = 5;

This code ran for me:

Code:
function c = mshvol(a, b, delta, FA, Hi, vHnew, iv, x)
   fa = f(a, FA, Hi, vHnew, iv, x);
   fb = f(b, FA, Hi, vHnew, iv, x);
while ( abs(b - a) > 2*delta )
   c = (b + a)/2;
   fc = f(c, FA, Hi, vHnew, iv, x);
    if sign(fc) ~= sign(fb)
      a = c; fa = fc;
    else
      b = c; fb = fc;
   end% end if
end

function fx = f(zbulk, FA, Hi, vHnew, iv, x)
dx=.001;
% x=0:dx:5;
y=25-x.^2;
diff = abs(x-zbulk);
[~, idx] = min(diff); %index of closest value
z=y(idx:end);
fx = zbulk*15+trapz(z)*dx-70; %% Volume balance
return;

Code:
c = mshvol(a, b, delta, FA, Hi, vHnew, iv, x)

c =

    8.7500

I added x in all the places you did, though, so I'm wondering why this doesn't run for you?

Can you copy/paste the entire error message you are seeing? It should be giving a line number where the error occurs that will be helpful.
 
That's weird that it's not working for me. The error I get is: Error using mshvol (line 35) Not enough input arguments. I have a lot of comments in my code, so my line 35 corresponds to line 2 from the code you posted, the one that works for you.

I defined ##x## from the command window, which seems like you did too, right?

Thanks a ton for helping!
 
Yes I defined x at the command line, along with the other variables in the first code block, before calling the function.

Can you just copy and paste your entire code verbatim so I can try to reproduce the error you're getting?
 
Definitely. It's below. In the command window I defined the following too:dx=.01;
x=0:dx:5;
a=0;
b=5;
delta=.00001;
FA=1;
Hi=1;
vHnew=1;
iv=1;

Code:
function c = mshvol(a, b, delta,FA,Hi,vHnew,iv,x)
% Remember that the function statement must be at the top of
% your m-file! Comments come after the function statement.
%
%
% mshvol.m
% 
% Implements the bisection method
%
% Input:     a     the left endpoint of the interval
%         b     the right endpoint of the interval
%         delta    the tolerance/accuracy we desire
%
% Output:    c     the approxmiation to the root of f
% 
% Syntax:    bisect(a, b, delta)
%
% Notes:  
%        1. The code defining f comes after the code defining
%            bisect, since bisect depends on f.
%        2. By default, MATLAB only displays 5 digits.  You can
%            change this by issuing the command "format long".
%            See "help format" for more details.
%% Initial Guess and Tolerance
clear zbulk;        %this is so zbulk is treated as a variable in the function
fa = f(a,FA,Hi,vHnew,iv,x);%% compute initial values of f(a) and f(b)
fb = f(b,FA,Hi,vHnew,iv,x); 

% if  sign(fa) == sign(fb)    %% sanity check: f(a) and f(b) must have different
%                 %% signs
%                 %% the error function prints an error message and
%                 %% exits
%     error('f must have different signs at the endpoints a and b.  Aborting.')
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                       %%
%% main routine                                                          %%
%%                                                                       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while ( abs(b - a) > 2*delta )    %% While the size of the interval is
                %% larger than the tolerance

    c = (b + a)/2;        %% Set c to be the midpoint of the interval

    fc = f(c,FA,Hi,vHnew,iv,x);        %% Calculate the value of f at c

    if sign(fc) ~= sign(fb)    %% If f(c) and f(b) are of different sign, then
                %% f must have a zero between c and b (by the 
                %% Intermediate Value Theorem).
        a = c;     fa = fc;
    else             %% This is the case where f(a) and f(c) are of 
                %% different sign
        b = c;    fb = fc;
    end
end
function fx = f(zbulk,FA,Hi,vHnew,iv,x)
    y=25-x.^2;
    diff = abs(x-zbulk);
    [~, idx] = min(diff); %index of closest value
    z=y(idx:end);
    fx = zbulk*15+trapz(z)*dx-70;  %% Volume balance
    return;
 
I don't get the error you mentioned. The only thing immediately wrong with the code you posted is that you define dx in the command window, but dx is used again in the nested function for f, so I got this error:

Code:
c = mshvol(a, b, delta, FA, Hi, vHnew, iv, x)
Undefined function or variable 'dx'.

Error in mshvol>f (line 62)
    fx = zbulk*15+trapz(z)*dx-70;  %% Volume balance

Error in mshvol (line 26)
fa = f(a,FA,Hi,vHnew,iv,x);%% compute initial values of f(a) and
f(b)

So I added dx=0.01 to that nested function, and then the code ran:

Code:
c = mshvol(a, b, delta, FA, Hi, vHnew, iv, x)

c =

    4.6198

Some general observations:

1. The value assigned to fa is unused in this code.
2. The function f is defined to accept 6 input arguments. But it does not use the values of FA, Hi, vHnew, or iv in any computations, so it really just needs zbulk and x. These variables aren't used anywhere else, either: so I'm wondering why mshvol needs 8 inputs instead of 4?
3. diff is the name of a MATLAB function, so when you assign a value to a variable named diff in the nested function f, you override that functionality.

Do you have multiple files named mshvol on the MATLAB path? Line 35 in the code you sent is in a comment, so I don't see how that error could be attributed to the code you sent. I suspect it is running a different file with the same name.
 
  • Like
Likes   Reactions: member 428835
I'm sorry, I actually am getting the following error (line 35 was when the variables were defined in the code):

Error using mshvol (line 26)
Not enough input arguments.

I'm defining all variables outside the function in the command window. For some reason this code works fine for scalars but then ##x## throws the entire thing off. As for the unused variables, I was planning on adding them in after I get the above code to work.

But any idea why this error NOT ENOUGH INPUT ARGUMENTS is showing up?

Thanks so much, you're very helpful!
 
It's working now! The issue was I was simply pressing "run" when i should have instead typed from the script of command window mshvol(a, b, delta,FA,Hi,vHnew,iv,x).

Thanks so much for your concern!
 
Ahhh ok that makes sense!

The run button is best reserved for simple scripts that are self contained. Since functions require inputs, you need to use the command window to explicitly specify those inputs.

I'm glad you figured it out.

Cheers,
Josh
 
  • Like
Likes   Reactions: member 428835
  • #10
Thanks kreil!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K