Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:26:55+00:00 2026-05-26T22:26:55+00:00

I am writing program in Octave and I encountered a problem, I implemented Gauss-Legandre

  • 0

I am writing program in Octave and I encountered a problem, I implemented Gauss-Legandre Quadrature and I pass to my Gauss-Legandre function few arguments, and I pass the function to be intergrated in a cell cube, so I can pass few function at a time. I have this piece of code:

    for weight=1:length(w)
        temp=1; 
        for fun=1:length(c)
            %liczenie iloczynu f(x_i)g(x_i), x_i - pieriwastki wielomianu Legandra
            f=c{fun};
            nargin(func2str(c{fun}))
            if (nargin (func2str(c{fun})) == 1)
                disp('a');
                temp*=c{fun}((b-a)/2 * x(weight) + (a+b)/2);
            else
                    (b-a)/2 * x(weight) + (a+b)/2;  
                temp*=f((b-a)/2 * x(weight) + (a+b)/2,I,points);
            end
        end
        %mnozenie wyniku przez odpowiedni wspolczynnik - wage
        temp*=w(weight);
        result+=temp;
    end

In cell array there are function handlers to functions which I want to integrate. Depending on number of arguments that function takes i want to use two different calls for function. If in cell array there is handler to a function that is written in .m file in the same directory as my Octave working directory everything works fine, but when i define function in Octave running time, for example:

    function result=a(x)
    result=x*x
    end

Type

    c{1}=@a

and pass this cell array to my function Kwadratury there is an error of nargin

    error: nargin: invalid function
    error: called from:

Why is that and how can I solve it, so I can define function not only in .m files but also in Octave.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:26:56+00:00Added an answer on May 26, 2026 at 10:26 pm

    I suspect I have a solution, but as this is Octave-specific and I’m mostly used to MATLAB, your mileage may vary.

    You call the nargin function by supplying a string argument, this means that nargin will have to resolve that function and check the number of arguments. When you declare a function in-line, that function is defined within that scope (i.e. your base scope), so resolving the function name will not work from within any function (or it might resolve to a built-in function, which is even worse behavior).

    The best solution is to use nargin(c{fun}) instead of nargin(func2str(c{fun})). That way you pass the actual function handle along, and there is no need to resolve the function name to the actual function, and hence no possible ambiguity.

    In general I recommend against using strings to pass functions: that why function handles are included in MATLAB, so anyone reading your code (or a static code analysis tool) will be able to understand you are working with functions. When using strings, everything becomes ambiguous: does a string 'a' refer to the function a or to the first letter in the alphabet?

    With regard to using inline functions, I don’t know whether Octave supports this, but if you function is quite simple, it’s easier to define an anonymous function, such as your example, by a = @(x)(x*x);. That is a construct that is supported by MATLAB, so that makes your code more portable to other environments (well, you’d still need to replace X *= A with X = X * A; to be MATLAB compatible).

    edit:
    Another possibility could be to just try out whether a form with multiple parameters works and fall back to the one parameter form when necessary:

    try
       (b-a)/2 * x(weight) + (a+b)/2;  
       temp*=f((b-a)/2 * x(weight) + (a+b)/2,I,points);
    catch ME
       try
           disp('a');
           temp*=c{fun}((b-a)/2 * x(weight) + (a+b)/2);
       catch ME
       end
    end
    

    You might want to check whether the returned error ME really states that a wrong number of arguments is used to allow other errors through. I do admit this is an ugly work-around, but since Octave apparently doesn’t support function handles for nargin, it might be the only way you’d get what you want for inline functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I have met problem with synchronizing threads. I am writing program which
Im writing a program which uses simple neural network perception algorithm, I have problem
Im writing a program that should read input via stdin, so I have the
I'm writing a program that sends an email out at a client's specific local
I am writing a program which has two panes (via CSplitter ), however I
If you are writing a program that is executable from the command line, you
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I'm writing a program to read from a POP3 mailbox and upload the email
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
I am writing a program in Python that will act as a server and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.