手把手MATLAB 建立函數文件 實現簡單 的積分 微分
序號 | 內容 |
---|---|
1 | 建立函數文件 |
2 | 實現簡單三角波的積分 |
3 | 實現簡單三角波的微分 |
一,建立函數文件
鍵入以下代碼,然後保存,保存的文件名字一定要和函數名字一樣
function yt = fun1(t)%函數名爲fun1
yt = tripuls(t,4,0.5);
end
二,實現簡單三角波的積分
h=0.001;t=-3:h:3;
y=fun1(t);
y1=diff(y)/h;
subplot(2,1,1);
plot(t,y);
title('f(t)');
subplot(2,1,2);
plot(t(1:length(t)-1),y1);
title('df(t)/dt')
三,實現簡單三角波的微分
t=-3:0.1:3;
y=tripuls(t,4,0.5);
for x=1:length(t)
y2(x)=quad('fun1',-3,t(x));
end
subplot(2,1,1);
plot(t,y);
title('f(t)');
subplot(2,1,2);
plot(t,y2);
title('integral of f(t)');