Sửa lỗi not enough input arguments matlab function năm 2024

I understand that you are getting the "not enough input arguments" error while executing the above code.

The "Not enough input arguments" error in MATLAB occurs when a function is called with fewer input arguments than it is defined to accept. To solve this error, you need to provide the required number of input arguments to the function.

I would suggest starting a debugging session and checking whether all the input arguments such as "new_N", "n_intfs" etc. are getting initialized before line 11 or not. This can be done by setting up break points before line 11 and checking out the variables present in the workspace at that instance.

Change the function argument of integral to the following:

q=integral[@[x]myfun1[x,t,L[i]],xmin,xmax];

The integral function requires integrating w.r.t a single variable, which is ‘x’ in this case and since ‘t’ is constant w.r.t the integral, it should not be mentioned as variable in the integral function’s function argument.

You can refer to the documentation of integral

I am writting codes to a s-function block to create demo or animate of a liquid Tank ,but I have a problem which is "not enough Arguments".I am not sure if you know what is the problem,I have paste my written codes below.

function [sys,x0,str,ts,setPt1,setPt2,waterx1,vessel1Hndl,vessel1Ht, vesselHndlList,waterY1,vessel2Hndl,lineHndl1,lineHndl2]=vesseldemo2project [u,flag,t,x] circumstances under which MATLAB will try to look outside of the current function to try to find a definition for a variable that is named as an input parameter to the current function.

If you do not pass a value as the first parameter to Out then MATLAB will never look in the calling script or function to determine whether a variable named Num happens to be defined there.

[There are limited circumstances under which MATLAB will search for variables in "outer" functions, but those circumstances never include variables defined in the function definition of the function that needs the variable.]

The place that invokes Out needs to call something like

Caution:

Your code does not define any value for x in the case that Num[1] is not '1'

Note by the way strncmpi[] is a case-INsensitive character vector comparison. It is useful, for example, in testing whether whether an input is either 'a' or 'A' . But there is no upper-case or lower-case version of the digit '1' so it is pointless to do a case-INsensitive test.

Your code will fail if the input Num is an empty vector. Interestingly, your code will not [directly] fail if the input is the scalar string "" -- in that case Num[1] would be the scalar string "" and strncmpi["", '1', 1] would be a valid test. I have this code. Even though I entered all 4 input arguments necessary, I'm still getting the error "not enough input arguments". Any help will be appriciated!

function [Mtrue, H0true] = fit_disL[z, M, H0, dL]

[Mtrue, H0true] = fminsearch[@error_disL, [M, H0]]

function E = error_disL[M, H0]

E = 1/length[z]*sum[[[disL[z, M, H0]].^2 - dL.^2].^0.5];

This is the error message:

Not enough input arguments.

Error in fit_disL/error_disL [line 4]

E = 1/length[z]*sum[[[disL[z, M, H0]].^2 - dL.^2].^0.5];

Error in fminsearch [line 201]

fv[:,1] = funfcn[x,varargin{:}];

Error in fit_disL [line 2]

[Mtrue, H0true] = fminsearch[@error_disL, [M, H0]]

Accepted Answer

The error message is actually saying there aren't enough inputs to the function error_disL [not fit_disL]. That happens because error_disL is defined to take 2 inputs, M and H0, but fminsearch is giving it 1 input, which is effectively the vector [M H0].

To fix this, you can redefine error_disL to take 1 input, a vector containing both M and H0 like fminsearch will give it:

function [Mtrue, H0true] = fit_disL[z, M, H0, dL]

[Mtrue, H0true] = fminsearch[@error_disL, [M, H0]]

function E = error_disL[MH0]

E = 1/length[z]*sum[[[disL[z, M, H0]].^2 - dL.^2].^0.5];

More Answers [1]

Edited: Jan on 17 Jun 2022

The function handle provided to fminsearch must accept 1 input argument. fminsearch cannot guess, that H0 should be provided also.

You have defined error_disL as a nested function. Then it shares the value of H0 with the enclosing function. So this should work:

function [Mtrue, H0true] = fit_disL[z, M, H0, dL]

[Mtrue, H0true] = fminsearch[@error_disL, [M, H0]]

function E = error_disL[x]

E = 1 / length[z] * sum[sqrt[disL[z, M, H0].^2 - dL.^2]];

Alternatively:

function [Mtrue, H0true] = fit_disL[z, M, H0, dL]

fcn = @[x] error_disL[x, z, dL];

[Mtrue, H0true] = fminsearch[fcn, [M, H0]];

function E = error_disL[x, z, dL]

E = 1 / length[z] * sum[sqrt[disL[z, M, H0].^2 - dL.^2]];

I've replaced the expensive ^0.5 by the cheaper sqrt[].

See Also

Categories

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Chủ Đề