How to create a function within a function ?

1 view (last 30 days)
i have two scripts with a function on each but i need the two functions to run on one script. My two functions are as follows:
function f = OscillationsofStr( x )
% Input:
% x = the co-ordinate of the point
% Data
% Output:
% f=x*sin(x)+cos(x)
f=x*sin(x)+cos(x);
end
function alpha=solEquation()
% Data
alphaIni=0;
alphaEnd=4;
% The oscillations of the Structure function
alpha=fzero(@OscillationsofStr,[alphaIni,alphaEnd],1);

Accepted Answer

A Jenkins
A Jenkins on 2 Apr 2014
If you want them to reside in one file, you need to switch the order, so that the calling (main) function is before the called (local) function.
function alpha=solEquation()
% Data
alphaIni=0; alphaEnd=4;
% The oscillations of the Structure function
alpha=fzero(@OscillationsofStr,[alphaIni,alphaEnd],1);
end
function f = OscillationsofStr( x )
% Input: % x = the co-ordinate of the point % Data
% Output: % f=x*sin(x)+cos(x)
f=x*sin(x)+cos(x);
end

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!