why when i run code this error fv(:,1) = funfcn(x,varargin{:}) ?

global Nt Lf
Nt = 14;
Lf = 3.00;
d = 0.081;
OD = 0.800;
x_start = [ d OD ];
[ x_solution, min_val ] = fminsearch( 'pr02_spring', x_start )
d = x_solution(1);
OD = x_solution(2)
rho = 0.28;
G = 11.7e6;
Lw = 1.5;
D = OD - d;
w = rho * ( pi*d*d/4 ) * (pi*D) * Nt;
ID = D - d;
Na = Nt - 2;
k = d*d*d*d*G / (8*D*D*D*Na);
Fw = k * ( Lf - Lw );
Ls = Nt * d;
Fs = k * ( Lf - Ls );
C = D / d;
Ks = 1 + 0.5/C;
tau = Ks*8*Fs*D / (pi*d*d*d);
Sut = 184649 * ( d ^ -0.1625 );
Sys = 0.45 * Sut;
Nfs = Sys / tau;
disp( ' d OD Nt Lf weight' )
disp( ' [inch] [inch] [inch] [lbf]' )
disp( [ d OD Nt Lf w ] )
disp( ' ID k Fw Ls Nfs' )
disp( ' [in] [lbf/in] [lbf] [inch]' )
disp( [ ID k Fw Ls Nfs ] )

7 Comments

What is the error. Post the full text, everything in red.
Most likely the error comes from pr02_spring for which you haven't supplied the code.
Undefined function or variable 'Pro2_spring'.
Error in fminsearch (line 200) fv(:,1) = funfcn(x,varargin{:});
Error in solution (line 8) [x_solution ,min_val] = fminsearch('Pro2_spring', x_start);
>>
Well, there you go! Where is the pr02_spring.m file?
Note that the 3rd character is a zero, not an oh.
[x_solution ,min_val] = fminsearch('w', x_start); i change it with w in m.file give me the same problem
@muhammad jassim: Guillaume asked, where the file pr02_spring.m is. Answering this question would allow to solve the problem. Using 'W' instead does not change anything and it is not clear, why you assume this.
can you give me your contact whatapp or email to sent you code to help me to solve it , deadline will kill me
Jan
Jan on 10 Jul 2018
Edited: Jan on 10 Jul 2018
Please do not close questions, which got an answer.
The nature of this forum is to share solutions in public. Therefore neither email or whatsapp is really welcome. You did not answer the questions for clarifications yet, so you do not let us you help.
I still assume the solution is very easy and you simply do not save the text of the functions, where it is expected. You can simply post your code here. But remember that the readers are not interested in this question anymore, because you have marked it as solved already by accepting an answer.

Sign in to comment.

 Accepted Answer

I don't even understand why you'd try to pass your variable w to fminsearch. The whole purpose of the fminsearch is to minimise a function. How is it going to achieve that if you pass it a variable?
The error message tells you that matlab can't find the function pr02_spring. That function should be its own m file, hence you should have a pr02_spring.m file which is the function you want to minimise.
So which function are you trying to minimise? What is its name? And where is the file where it is defined?

10 Comments

frist question wants me to use fminsearch can you help me
Yes, of course Guillaume can help you, if you answer his questions for clarifications.
On page 5 of your pdf, you hav the code for pr02_spring.m. Didn't you write that code? I don't understand how you can write the code for your assignment, yet are not able to use it properly.
That pr02_spring.m file needs to be in your matlab search path. Easiest is to put it in your current directory. The error will then go away.
Jan
Jan on 10 Jul 2018
Edited: Jan on 10 Jul 2018
The PDF is from 2014, the code was written 2016.
@muhammad: Guillaume asked: "So which function are you trying to minimise? What is its name? And where is the file where it is defined?" This is the core of the problem.
Not enough input arguments.
Error in solution (line 3) d = x(1); % wire DIA [in]
Not enough input arguments.
I'm not sure what to say here. It's clear that you have absolutely no idea how to use the code. The only thing I can advise is to go through the matlab tutorials, and go through your course notes.
If you're getting this error it's because somehow you're running pr0-spring.m directly. The error will not occur if the function is called by fminsearch since fminsearch will pass the correct number of inputs (and of the right size as well)
this page is hella mean

Sign in to comment.

More Answers (1)

Provide the function handle to the existing function as 1st input to fminsearch. Using a char vector is allowed for backward compatibility, but this is outdated for 20 years now.
Simply replace 'pr02_spring' by e.g. @yourFcn, where "yourFcn" is the name of a function inside Matlab's path. See also:
doc addpath
doc pathtool

2 Comments

i did it also doesn`t work
What is "it"? Defining a valid function handle is the only reliable way. If you state, that it does not work, show us, what you have tried and which error message you get. What did you used instead of "yourFcn"?
You did not answer Guillaume's questions for clarifications also. But now you have selected his answer as "accepted", which means, that the problem is solved. Is it?

Sign in to comment.

Categories

Find more on Function Creation 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!