简体   繁体   中英

creation of function handle in matlab

N= input('nodes');
e5= ones(N,1);
e4= ones((N-1),1);
A = -2*diag(e5)+diag(e4,1)+diag(e4,-1)
k = sym('k',[N 1])
s = A*k;
f = (A*k)-(sign(k).*((k).^2))
g = diff(f,k)

The problem is that the code seen above creates a function with the set of input arguments (k1,k2) instead of (k). and also not running differentiation command.

Use f=@(k) :

f = @(k)A*k-sign(k).*k.^2
f([3; 2])

To differentiate, first use sym(f) . Treating k as a scalar:

diff(sym(f))

which gives, in this case

[ - 2*k*sign(k) - 2*k^2*dirac(k) - 2,   1 - 2*k^2*dirac(k) - 2*k*sign(k)]
[   1 - 2*k^2*dirac(k) - 2*k*sign(k), - 2*k*sign(k) - 2*k^2*dirac(k) - 2]

But I don't think that is what you want. Perhaps you are looking for diff(sym(f([k1;k2])),k1) and diff(sym(f([k1;k2])),k2) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM