Task 07:习题
>> y=inline('sin(x)/(1+3*x+x*x)','x')
y =
内联函数:
y(x) = sin(x)/(1+3*x+x*x)
3-2
编辑器:
function Z=Z(x,x1,x2,x3)
Z=x1^2+2*x2^2+3*x^2+2*x1*x2+3*x2*x3+5*x2*x3;
命令窗口:
>> Z=Z(1,2,3,4)
Z =
133
3-3
%计算逆序数
clear
x=input('please input x:');
str=num2str(x);
for i=1:length(str)
s(i)=str2double(str(i));
end
if length(s)==1
disp(0);
else
for j=2:length(s)
s1=find(s(1:j-1)>s(j));
s2(j)=length(s1);
S=sum(s2);
end
disp(S);
end
>> nxx
please input x:84723536
16
>> nxx
please input x: 52397899
4
3-4
A=input('请输入矩阵:');
a=max(max(A));
b=min(min(A));
fprintf('最大值为:%d,最小值为:%d\n',a,b);
disp('最大值下标:');
[row,col]=find(A==a)
disp('最小值下标:');
[row,col]=find(A==b)
命令:
>> row_col
请输入矩阵:[3 4 4;5 2 1;6 6 6]
最大值为:6,最小值为:1
最大值下标:
row =
3
3
3
col =
1
2
3
最小值下标:
row =
2
col =
3
3-5
V=input('please input:');
[Y,I]=sort(V);
m=max(I);
n=round(m/2);
n1=round(m/2)+1;
if mod(m,2)==0
x=(Y(n)+Y(n1))/2;
fprintf('中位数为:%d\n',x)
else
y=Y(n);
fprintf('中位数为:%d\n',y)
end
命令:
>> med
please input:[3 2 0 7 1]
中位数为:2