MATLAB: How to define a function inside a function

functions

hello i hava defined a function:
function jj= jj1(C1, C2, C3, C4, k, q, P,gg, x) jj = C1*cos(k*x)+C2*sin(k*x)+C3*x+C4+gg
the thing is that gg is also a function:
gg1=gg(q,P,x) gg1=q/(2*P)*x^2
obviously this is most likely to be wrong because I have no idea how to do it. Can anyone give me some help please. i put the editor as attachemnent.

Best Answer

Hi,
if gg is a function you will need to create it and pass to jj1. You can e.g. create an anonymous function:
gg = @(q,P,x) q./(2*P)*x.^2;
% now call jj1:
result = jj1(C1, C2, C3, C4, k, q, P, gg, x);
% and inside jj1:
gg1 = gg(q, P, x);
or you create a seperate .m file:
function gg1 = gg(q, P, x)
gg1 = gg(q, P, x);
and now pass the function handle to jj1:
result = jj1(C1, C2, C3, C4, k, q, P, @gg, x);
Titus
Related Question