一、
题目:
建立一个脚本文件,计算10!
解答代码
a=1;
for i=1:10
a=a*i;
end
jieguo=a
运行脚本
二、
题目:
建立一个函数文件,定义阶乘函数,然后再命令窗口计算10!
我们保存的函数文件代码如下(建立m文件)
function jiecheng(n)
a=1;
jieguo=0;
for i=1:n
a=a*i;
jieguo=a;
end
fprintf('\n %0.0f 的阶乘等于',n)
fprintf('%0.0f\n\n',jieguo)
end
命令行输入
三、
题目:
建立函数文件,定义函数y=x-x^3/3!+x^5/5!-x^7/7!,然后在命令窗口计算y(1)到y(15)的值,并保存到exce表格里面
操作如下
①建立函数文件
function y=fun(x)
y=x-x.^3/factorial(3)+x.^5/factorial(5)-x.^7/factorial(7);
end
②在命令行输入x值
③计算
④写入数据,很简单用xlswrite函数
xlswrite(filename, M, sheet, range)
后面为文件名,变量名,行数,区域
这里给一个反面教材
结果我们就把变量保存为了这样的xlsx文件中
四、
题目:
在同一个坐标系内,做出函数y=sinx,y1=x,y2=x-x^3/3!,y3=x-x^3/3!+x^5/5!,y4=x-x^3/3!+x^5/5!-x^7/7!的图像,并标注图像名称和谁是谁的图像
代码如下
clear,clc
x=-2*pi:0.01:2*pi;
y1=sin(x);
y2=x
y3=x-(x.^3)/factorial(3);
y4=x-(x.^3)/factorial(3)+(x.^5)/factorial(5);
y5=x-(x.^3)/factorial(3)+(x.^5)/factorial(5)-(x.^7)/factorial(7);
plot(x,y1,x,y2,x,y3,x,y4,x,y5);
title('函数图像');
text(0.2,0.8,'y1=sinx');
legend('y1','y2','y3','y4','y5');
效果为
五、
题目:
在同一个图像幕布内,按照2*3做出函数y=sinx,y1=x,y2=x-x^3/3!,y3=x-x^3/3!+x^5/5!,y4=x-x^3/3!+x^5/5!-x^7/7!,
% y5=x-x^3/3!+x^5/5!-x^7/7!+x^9/9!的图像,并标注图像名称.
clear,clc
x=-2*pi:0.01:2*pi;
y1=sin(x);
y2=x
y3=x-(x.^3)/factorial(3);
y4=x-(x.^3)/factorial(3)+(x.^5)/factorial(5);
y5=x-(x.^3)/factorial(3)+(x.^5)/factorial(5)-(x.^7)/factorial(7);
y6=x-(x.^3)/factorial(3)+(x.^5)/factorial(5)-(x.^7)/factorial(7)+(x.^9)/factorial(9);
subplot(2,3,1),plot(x,y1),title('sin(x)')
subplot(2,3,2),plot(x,y2),title('x')
subplot(2,3,3),plot(x,y3),title('x-(x.^3)/factorial(3)')
subplot(2,3,4),plot(x,y4),title('x-(x.^3)/factorial(3)+(x.^5)/factorial(5)')
subplot(2,3,5),plot(x,y4),title('x-(x.^3)/factorial(3)+(x.^5)/factorial(5)-(x.^7)/factorial(7)')
subplot(2,3,6),plot(x,y4),title('x-(x.^3)/factorial(3)+(x.^5)/factorial(5)-(x.^7)/factorial(7)+(x.^9)/factorial(9)')