示例:求函数 的导函数并画出两个函数的图像。
clear
clc
close all
%%
% 创建符号变量sx
syms sx
% 创建关于符号变量sx的符号函数sf
sf=1/(1+exp(-sx));
% 将符号函数sf转化为普通函数f
f=matlabFunction(sf);
% 对符号函数sf求得导函数sdf
sdf=diff(sf);
% 将导函数的符号函数形式sdf转化为普通形式的函数
df=matlabFunction(sdf)
%% 自变量网格(画图用)
t=linspace(-10,10,100);
% 画图:函数图像和导函数图像
hold on
plot(t,f(t),LineWidth=2)
plot(t,df(t),LineWidth=2)
legend('f','df')
hold off
% axis equal
输出:
df =
包含以下值的 function_handle:
@(sx)exp(-sx).*1.0./(exp(-sx)+1.0).^2
图像: