MatLab学习作业之多项式与数值微积分(台大郭彦甫)

Practice
一:Plot the polynomial ?(?) = (5?3 − 7?2 + 5? + 10)(4?2 + 12? − 3) and its derivative for −2 ≤ ? ≤ 1
%第一个练习不是很难
%用conv实现两多项式的乘法
%在运用handle将图片加粗,唯一不明白在于图像legend中英文 ’ 的输入不太懂,因为会与本身语法 ‘ ’冲突
u=[5 -7 5 10];
v=[4 12 -3];
h=conv(u,v);
x=-2:0.01:1
f=polyval(h,x);
f1=polyval(polyder(h),x);
p=plot(x,f,‘b–’,x,f1,‘r-’);
set(p,‘LineWidth’,2)
xlabel(‘x’)
legend(‘f(x)’,‘f’(x)’)
二:Given ?(?) = sin(?), write a script to find the error of ?′(?0) at ?0 = ?/2 using various ℎ
%运用for循环实现
%导数的求通过定义求得运用 diff函数(后减前)
clc
clear all
x0 = pi/2;
i=1;
h=0.1;
error=ones(1,7);
for i=1:1:7
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
error(i)= diff(y)./diff(x)-0
h=h.0.1
end
三:Given ?(?) = ?−? sin(?2/2), plot the approximate derivatives ?′ of ℎ = 0.1, 0.01, and 0.001
%运用彩色标定,新函数colormap
%循环从定义入手得到导数画图
g = colormap(lines); hold on;
for i=1:3
x = 0:power(10, -i):2
pi;
y = exp(-x).*sin((x.x)/2); m = diff(y)./diff(x);
plot(x(1:end-1), m, ‘Color’, g(i,:));
end
hold off;
set(gca, ‘XTick’, 0:pi/2:2
pi);
set(gca, ‘XTickLabel’, {‘0’, ‘π/2’, ‘π’,‘3π/2’,‘2π’});
h = legend(‘h=0.1’,‘h=0.01’,‘h=0.001’);

你可能感兴趣的:(Matlab学习)