
3-1
>> y=inline('sin(x)/(1+3*x+x*x)','x')
y =
内联函数:
y(x) = sin(x)/(1+3*x+x*x)
>> y(pi/2)
ans =
0.1223
3-2
编写的函数
function Z=relation1(x,x1,x2,x3)
x=input('please input x: ')
x1=input('please input x1: ')
x2=input('please input x2: ')
x3=input('please input x3: ')
Z=x1^2+2*x2^2+3*x^2+2*x1*x2+3*x2*x3+5*x3*x
运行结果
>> relation1
please input x: 10
x =
10
please input x1: 11
x1 =
11
please input x2: 12
x2 =
12
please input x3: 13
x3 =
13
Z =
2091
ans =
2091
3-3
m文件
%计算逆序数
n=input('please input the number: ');
m=n;
k=1;
number1=0;
while m>0
a(k)=mod(m,10);
k=k+1;
m=fix(m/10);
end
k=k-1;
for i=1:k
b(i)=a(k-i+1);
end
for i=1:k-1
for j=i+1:k
if(b(i)>b(j))
number1=number1+1;
end
end
end
number1
结果
>> invertionnumber
please input the number: 4123
number1 =
3
3-4
程序
%seak for the min and max
A=rand(4,3);
min=A(1);
max=A(1);
for i=1:4*3
if(A(i)>max)
max=A(i);
end
if(A(i)<min)
min=A(i);
end
end
max,min
结果
>> seak_for_the_min_and_max
max =
0.9595
min =
0.0357
3-5
程序
%search_for_the_median
B=size(A);
n=B(2);
if mod(n,2)==1
m=(n+1)/2;
median1=A(m)
else
m=n/2;
median1=(A(m)+A(m+1))/2
end
结果
>> A=[1,2,3,4,5,6,7,8]
A =
1 2 3 4 5 6 7 8
>> search_for_the_median
median1 =
4.5000
3-6
程序
%order_n_matrix
n=input('n= ');
for i=1:n
for j=1:n
number1=(i-1)*n+j;
if(j==n)
fprintf('%g \n',number1);
else
fprintf('%g ',number1);
end
end
end
结果
>> order_n_matrix
n= 3
1 2 3
4 5 6
7 8 9
3-8
程序
%search_for_prime_number
n=1100;
s=0;
for j=1:n
k=0;
for i=1:j
if(mod(j,i)==0)
k=k+1;
end
end
if( k==2)
s=s+1;
if(mod(s,10)==0)
fprintf('%8g\n',j);
else
fprintf('%8g',j);
end
end
end
fprintf('\n');
结果
>> search_for_prime_number
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601
607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733
739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997 1009 1013
1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
1087 1091 1093 1097
3-9
程序
%Rxy
sxx=0;
syy=0;
sxy=0;
W=size(A);
n=W(2);
x1=mean(A);
y1=mean(B);
for i=1:n
sxx=sxx+(A(i)-x1)^2;
end
for i=1:n
syy=syy+(B(i)-y1)^2;
end
for i=1:n
sxy=sxy+(A(i)-x1)*(B(i)-y1);
end
fprintf('Rxy= %f\n',sxy/sqrt(sxx*syy));
结果
>> A,B
A =
1 2 3 4
B =
5 6 7 45
>> Rxy
Rxy= 0.800377
本文集展示了MATLAB在解决数学问题中的应用,包括函数定义、矩阵操作、素数搜索、统计数据处理等,通过具体实例深入浅出地介绍了MATLAB的编程技巧。

1596

被折叠的 条评论
为什么被折叠?



