✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
⛄ 内容介绍
1.1语法
[dy1,dy2,x1c,dy1c]=导数NE(x,y)
1.2输入
x 作为列或行向量,超过 2 个值
y 作为列向量或行向量,与 x 的值数量相同
1.3输出
dy1:一阶导数
dy2:二阶导数
x(1) 处的第一个 dy2 和 x(end) 处的最后一个 dy2 不存在并设置为 NaN
x1c 和 dy1c 返回精确的一阶导数 dy1c(x1c)
如果 x 或 y 是行向量,则所有输出都作为行向量返回。
出错时,所有输出都返回 NaN。
1.4语法示例
对于一阶导数:
dy1=导数NE([1;3;4;5],[1;9;16;25]);
对于二阶导数:
[~,dy2]=导数NE([1;3;4;5],[1;9;16;25]);
对于更准确的一阶导数:
[~,~,x1c,y1c]=derivativeNE([1;3;4;5],[1;9;16;25]);
⛄ 完整代码
%Demo for 'derivativeNE.m'%After 5 clicks on 'run' you get the results for sine curves%No plot for dy1c(x1c), since dy1c is nearly on top of dy1clcclose all%Create parbola or sineif exist('curveTypeCounter','var')curveTypeCounter=curveTypeCounter+1;if curveTypeCounter>10curveTypeCounter=1;endelseclearcurveTypeCounter=1;endcurveType=(curveTypeCounter>5);% curveType=1;%0=force parabola, 1=force sine%Create parbola or sinen=round(rand(1)*150+10);%Number of samples, min=10, max=160% n=20;%Force n samplesif curveType==0%Parabola with spike at x=0xr=rand(n,1)*2;x=sort(xr)-1;%create spikeindxL=find(x<-0.1,1,'last');indxR=find(x>0.15,1,'first');x=[x(1:indxL);-0.1;0;0.15;x(indxR:end)];x(1)=-1;x(end)=1;y=x.^2;y(indxL+2)=0.05;%Spike amplitudelegendText1=sprintf('y=x^{2} with spike at x=0');else%Sinexr=rand(n,1)*2*pi;x=sort(xr);x(1)=0;x(end)=2*pi;y=sin(x);legendText1='y=sin(x)';end[dy1,dy2,x1c,dy1c]=derivativeNE(x,y);%Calculate derivatives%Plot resultsnl=char(10);%new line, compatible to older Matlab releaseshfig=figure(1);clfhfig.Position(3)=hfig.Position(4)*1.8;yyaxis leftp1=plot(x,y,'.-g');% original curvexlabel('x')if curveType==0%Parabolaylabel('y = x^2 and spike at x=0')xticks([-1,-.5,-.1,0,.15,.5,1])text(1.27,0.5,['The exact derivates' nl 'at x=-0.1, 0, 0.15' nl 'are not defined as numbers.' nl ...'They are for the 1st derivative' nl 'Heaviside functions' nl ...'and for the 2nd derivative' nl 'Dirac functions.'],'BackgroundColor',[1,.85,.85],'FontSize',8)yyaxis rightxexact=-1:0.01:1;dy1exact=2*xexact;p2=plot(xexact(1:91),dy1exact(1:91),'color',[.7,.7,1],'LineWidth',4);%left part exact 1st derivativehold onindxx0=find(x==0);dyL=(y(indxx0)-y(indxx0-1))/(x(indxx0)-x(indxx0-1));dyR=(y(indxx0+1)-y(indxx0))/(x(indxx0+1)-x(indxx0));plot(xexact([91,91,101,101]),[dy1exact(91),dyL,dyL,dyR],'-','color',[.7,.7,1],'LineWidth',4);%left part of spike exact 1st derivativeplot(xexact([101,116,116]),[dyR,dyR,dy1exact(116)],'-','color',[.7,.7,1],'LineWidth',4);%right part of spike exact 1st derivativeplot(xexact(116:end),dy1exact(116:end),'-','color',[.7,.7,1],'LineWidth',4);%right part exact 1st derivativep3=plot(xexact([1,90]),[2,2],'-','color',[1,.7,.8],'LineWidth',4);%left part exact 2nd derivativeplot(xexact([92,115]),[0,0],'-','color',[1,.7,.8],'LineWidth',4);%left and right part of spike exact 2nd derivativeplot(xexact([117,end]),[2,2],'-','color',[1,.7,.8],'LineWidth',4);%right part exact 2nd derivative%Plot Diracsquiver(xexact(91),0,0,8,'-','Color',[1,0.7,0.7],'LineWidth',2.5,'MaxHeadSize',.06)quiver(xexact(101),0,0,-6,'-','Color',[1,0.7,0.7],'LineWidth',2.5,'MaxHeadSize',.08)quiver(xexact(116),0,0,8,'-','Color',[1,0.7,0.7],'LineWidth',2.5,'MaxHeadSize',.06)p4=plot(x,dy1,'b-');%1st numerical derivative interpolated% p4=plot(x1s,dy1c,'.y');%1st derivative centeredp5=plot(x,dy2,'+r-');%2nd numerical derivativelegend([p1,p4,p2,p5,p3],{sprintf('y=x^{2} with spike at x=0'),'1st derivative numerical','1st derivative exact',...'2nd derivative numerical','2nd derivative exact'},'Location','northeastoutside')ylim([-8,12])%Calculate Mean Absolute Errors and max errors without range of spike%1st derivative at xx4MAEdy1=[x(1:indxx0-2);x(indxx0+2:end)];% x for MAEdy14MAE= [dy1(1:indxx0-2);dy1(indxx0+2:end)];% dy1 for MAEdy1AD=abs(dy14MAE-2*x4MAEdy1);% Absolute differences[dy1Max,dy1Maxindx]=max(dy1AD);% Max absolute difference of dy1-dy1exactdy1MAE= sum(dy1AD)/numel(x4MAEdy1);% MAE dy1%1st derivative at x1c including spikex4MAEdy1c=[x1c(1:indxx0-2);x1c(indxx0+1:end)];% x for MAEdy1c4MAE= [dy1c(1:indxx0-2);dy1c(indxx0+1:end)];% dy1c for MAEdy1cAD=abs(dy1c4MAE-2*x4MAEdy1c);% Absolute differences[dy1cMax,dy1cMaxindx]=max(dy1cAD);% Max absolute difference of dy1c-dy1exactdy1cMAE= sum(dy1cAD)/numel(x4MAEdy1c);% MAE dy1c%2nd derivativex4MAEdy2=[x(2:indxx0-3);x(indxx0+3:end-1)];% x for dy2 MAEdy24MAE=[dy2(2:indxx0-3);dy2(indxx0+3:end-1)];% dy2 for MAEdy2AD=abs(dy24MAE-2);% Absolute differences[dy2Max,dy2Maxindx]=max(dy2AD);% Max absolute difference of dy2-dy2exactdy2MAE=sum(dy2AD)/numel(x4MAEdy2);% MAE dy2%Output text for MAEtext(1.27,-6,['Mean Abs. Error Max abs. error' nl ...'dy1: ' num2str(dy1MAE,'%.1e') ' ' num2str(dy1Max,2) ' @x=' num2str(x4MAEdy1(dy1Maxindx),2) nl ...'dy1c: ' num2str(dy1cMAE,'%.1e') ' ' num2str(dy1cMax,2) ' @x=' num2str(x4MAEdy1c(dy1cMaxindx),2) nl ...'dy2: ' num2str(dy2MAE,'%.1e') ' ' num2str(dy2Max,2) ' @x=' num2str(x4MAEdy2(dy2Maxindx),2) nl ...'Range around spike is excluded'], ...'BackgroundColor',[0.98,.98,.98],'FontSize',8)else%Sineylim([-1.05,1.05])ylabel('y = sin(x)')xticks(0:pi/2:2*pi+eps)set(gca,'TickLabelInterpreter','latex','XTickLabel',{'0','$\frac{\pi}{2}$','$\pi$','$\frac{3\pi}{2}$','$2\pi$'})% set(gca,'XMinorGrid','on')%Only for testyyaxis rightxexact=linspace(0,2*pi,200);p2=plot(xexact,cos(xexact),'color',[.8,.8,1],'LineWidth',4);% exact 1st derivativehold onp3=plot(xexact,-sin(xexact),'-','color',[1,.8,0.8],'LineWidth',4);% exact 2nd derivativep4=plot(x,dy1,'b-');% 1st numerical derivative interpolated% p4=plot(x1s,dy1c,'y-');%1st derivative centeredp5=plot(x,dy2,'+r-');% 2nd numerical derivativelegend([p1,p4,p2,p5,p3],{'y = sin(x)','1st derivative numerical','1st derivative exact',...'2nd derivative numerical','2nd derivative exact'},'Location','northeastoutside')ylim([-1.05,1.05])xlim([0,2*pi])%Calculate Mean Absolute Errors and max errors%1st derivative at xdy1AD=abs(dy1-cos(x));% Absolute differences[dy1Max,dy1Maxindx]=max(dy1AD);% Max absolute difference of dy1-dy1exactdy1MAE= sum(dy1AD)/numel(x);% MAE dy1%1st derivative at x1cdy1cAD=abs(dy1c-cos(x1c));% Absolute differences[dy1cMax,dy1cMaxindx]=max(dy1cAD);% Max absolute difference of dy1c-dy1exactdy1cMAE= sum(dy1cAD)/numel(x1c);% MAE dy1c%2n derivativex4MAEdy2=x(2:end-1);% x for dy2 MAEdy24MAE=dy2(2:end-1);% dy2 for MAEdy2AD=abs(dy24MAE+sin(x4MAEdy2));% Absolute differences[dy2Max,dy2Maxindx]=max(dy2AD);% Max absolute difference of dy2-dy2exactdy2MAE=sum(dy2AD)/numel(x4MAEdy2);% MAE dy2%Output text for MAEtext(7.2,0,['Mean Abs. Error Max abs. error' nl ...'dy1: ' num2str(dy1MAE,'%.1e') ' ' num2str(dy1Max,'%.1e') ' @x=' num2str(x(dy1Maxindx),2) nl ...'dy1c: ' num2str(dy1cMAE,'%.1e') ' ' num2str(dy1cMax,'%.1e') ' @x=' num2str(x1c(dy1cMaxindx),2) nl ...'dy2: ' num2str(dy2MAE,'%.1e') ' ' num2str(dy2Max,'%.1e') ' @x=' num2str(x4MAEdy2(dy2Maxindx),2)], ...'BackgroundColor',[0.98,.98,.98],'FontSize',8)endylabel('1st and 2nd derivatives')hold offgrid on
function [dy1,dy2,x1c,dy1c]=derivativeNE(x,y)%Calculate the 1st and 2nd derivative for Not Equal spaced samples.%%INPUT% x as column or row vector, more than 2 values% y as column or row vector, same amount of values as for x%%OUTPUT% dy1: 1st derivative, y'(x)%% dy2: 2nd derivative, y"(x)% The first dy2 at x(1) and the last dy2 at x(end) don't exist and are set to NaN% x1c and dy1c return a more accurate (about 3 times better) 1st derivative dy1c(x1c), but with new x-values%% If x or y is a row vector, all outputs are returned as row vectors.% On error NaN is returned%%Syntax examples:% dy1=derivativeNE([1;3;4;5],[1;9;16;25]); % 1st derivative y'(x)% [~,dy2]=derivativeNE([1;3;4;5],[1;9;16;25]); % 2nd derivative y"(x)% [~,~,x1c,y1c]=derivativeNE([1;3;4;5],[1;9;16;25]); % 1st derivative more accurate (about 3 times), y'(x1c)%%Peter Seibold, August 2023%Check inputsif size(x,2)>1 || size(y,2)>1%Either x or y is a row vectorisRowVector=true;x=x(:);%convert to column vectory=y(:);elseisRowVector=false;endif numel(x)~=numel(y) || numel(x)<3dy1=NaN; dy2=NaN; x1c=NaN; dy1c=NaN;%Return all output as NaNdisp('x and y must have the same size and at least 3 elements!')returnend%1st derivativedx=diff(x);dy=diff(y);dy1c=dy./dx;% 1st derivative for centered xx1c=x(1:end-1)+dx/2;% center each x (shift each x by half distance to next x)%interpolate/extrapolate dy1c(x1c) to match x, result: dy1(x)y2=[dy1c(2);dy1c(2:end);dy1c(end)]; %repeat border values for extrapolationy1=[dy1c(1);dy1c(1:end-1);dy1c(end-1)];x1=[x1c(1);x1c(1:end-1);x1c(end-1)];x2=[x1c(2);x1c(2:end);x1c(end)];dy1=(y2-y1)./(x2-x1).*(x-x2)+y2; %interpolate/extrapolate%2nd derivativey32x32=dy1c(2:end); %1st derivatives at even positionsy21x21=dy1c(1:end-1); %1st derivatives at odd positionsdx31=y32x32; %create vector with dummy values, with two elements less than number of x-elementsdx31(1:2:end)=x(3:2:end)-x(1:2:end-2);%x-difference odd pairsdx31(2:2:end)=x(4:2:end)-x(2:2:end-2);%x-difference even pairsdy2c=2*(y32x32-y21x21)./dx31; %2nd derivatives dy2 at center of the two surrounding samplesx2c=dy2c; %create vector with dummy values, with two elements less than number of x-elementsx2c(1:2:end)=(x(1:2:end-2)+x(3:2:end))/2;%x for dyc at center of the two surrounding samples, (overwrite dummy values)x2c(2:2:end)=(x(2:2:end-2)+x(4:2:end))/2;%dito for even x centered, now we have all center x2c for 2nd derivatives%interpolate dy2c(x2c) to match x, result: dy2(x)if numel(x)>3dy2=[NaN;interp1(x2c,dy2c,x(2:end-1),'linear','extrap');NaN];%interpolate and pad unavailable border valueselse%Interpolation needs at least two dy2c samplesdy2=[NaN;dy2c;NaN];% output only the single dy2(x)end%Convert back to user formatif isRowVector%Either x or y input was a row vector%Convert to row vectordy1=dy1';dy2=dy2';x1c=x1c';dy1c=dy1c';end
⛄ 运行结果

⛳️ 代码获取关注我
❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料
🍅 仿真咨询
1 各类智能优化算法改进及应用
生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化
2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断
2.图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知
3 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化
4 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配
5 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化
6 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化
7 电力系统方面
微电网优化、无功优化、配电网重构、储能配置
8 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长
9 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合
该博客作者是Matlab仿真开发者,介绍了Matlab导数函数的语法、输入输出及示例,还给出完整代码和运行结果。同时提供仿真咨询,涵盖智能优化算法、机器学习、图像处理、路径规划、无人机应用等多个领域。
1万+

被折叠的 条评论
为什么被折叠?



