第六章 MATLAB IN ENGINEERING
Polynomial Differentiation多项式微分
Polynomial Integration多项式积分
polyint()
Numerical Differentiation
diff()
//h的大小对于导函数的影响:
g = colormap(lines);
hold on;
for i = 1:4
x = 0:power(10,-i):pi;
y = sin(x);
m = diff(y)./diff(x);
plot(x(1:end-1),m,'Color',g(i,:));
end
hold off;
set(gca,'XLim',[0,pi/2]);
set(gca,'YLim',[0,1.2]);
set(gca,'FontSize',18);
set(gca,'FontName','symbol');
set(gca,'XTick',0:pi/4:pi/2);
set(gca,'XTickLabel',{'0','p/4','p/2'});
h = legend('h = 0.1','h = 0.01','h = 0.001','h = 0.0001');
set(h,'FontName','Times New Roman');
box on;
Second and Third Derivatives
x = -2:0.005:2;
y = x.^3;
m = diff(y)./diff(x);
m2 = diff(m)./diff(x(1:end-1));
plot(x,y,x(1:end-1),m,x(1:end-2),m2);
xlabel('x','FontSize',18);
ylabel('y','FontSize',18);
legend('f(x) = x^3','f''(x)','f''''(x)');
set(gca, 'FontSize',18);
Numerical Integration
Midpoint Rule用矩形去逼近
h = 0.05;
x = 0:h:2;
midpoint = (x(1:end-1)+x(2:end))./2;
y = 4*midpoint.^3;
Trapezoid Rule用梯形去逼近
trapz();
Simpson's Rule
h = 0.05;
x = 0:h:2;
y = 4*x.^3;
s = h/3*(y(1)+2*sum(y(3:2:end-2))+4*sum(y(2:2:end))+y(end))
Funtion Handles(@)
function [y] = xy_plot(input,x) %丢入类似@sin ; 一个函数不能作为input
% xy_plot receives the handle of a function and plots that
%function of x
y = input(x);
plot(x,y,'r--');
xlabel('x');
ylabel('function(x)');
end
integral();
eg:
y = @(x) 1/(x.^3-2*x-5);
integral(y,0,2) %0,2是上下限
Double and Triple Integrals
eg1:
f = @(x,y) y.*sin(x)+x.*cos(y);
integral2(f,pi,2*pi,0,pi) %二重积分,顺序跟书写的一样
eg2:
f = @(x,y,z) y.*sin(x)+z.*cos(y);
integral3(f,0,pi,0,1,-1,1);