error: Function definition is misplaced or improperly nested error?

조회 수: 18 (최근 30일)
I am defining a nested function (bicomp1) inside a function (biaxialcomp) whose output is to be minimised using the fmincon optimization function, but I get an error that this function is misplaced or improperly nested. What could be the problem with it?
function [Mux, Muy] = biaxialcomp(c,app,gc,gs,bar,n,i)
th = [10 20 30 40 50 60 70 80];
Mux = zeros(1,8);
Muy = zeros(1,8);
for iv = 1:8
% first potential scenario
if app.b*tand(th(iv)) < app.t
tt = app.b*sind(th(iv))+app.t*cosd(th(iv));
c_opt = fmincon(@bicomp1,c,[],[],[],[],0,tt);
function obj = bicomp1(c)
a = 0.8*c;
Xna = a/sind(th(iv));
fcu = 0.67*app.Fcu/gc;
...

채택된 답변

Stephen23
Stephen23 2020년 1월 31일
편집: Stephen23 2020년 1월 31일
"What could be the problem with it?"
This:
if ...
...
function obj = bicomp1(c)
Function definitions cannot be nested inside control statements. To get the effect you are looking for, you will have to parameterize your function (i.e. either use a nested function or an anonymous function with the required parameters):

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by