x=input('please input x:'); y=0; if x<0 y=0; elseif x<1 y=x; elseif x<=2 y=2-x; else y=0 end
½á¹û£ºplease input x:-3
ans =0
please input x:2^(1/2) ans =0.5858
please input x:inf y=0
ans = 0
3£®Óà for Ñ»·Óï¾äʵÏÖ±àдһ¸öÇón ½×³ËµÄº¯ÊýÎļþ 1¡¢º¯ÊýÎļþÉè¼Æ£º
Éè¼ÆÒ»¸öº¯ÊýÎļþʵÏÖÒ»¸ö½×³ËÔËËãy(n)?n!?1?2?3???(n?1)?n£¬
²¢Éè¼Æ³ÌÐòµ÷Óøú¯Êý¡£
Ϊ±£Ö¤º¯ÊýµÄͨÓÃÐÔ£¬µ±ÊäÈ븺Êý»òСÊýʱ£¬ÏÔʾ³ö´íÌáʾ£º disp('Input parameter must be a positive integer!') Ìáʾ£ºfix(x) ¶ÔÁã·½ÏòÈ¡ÕûÊý ceil(x) ¶Ô+?·½ÏòÈ¡ÕûÊý
round(x) ËÄÉáÎåÈëÈ¡ÕûÊý ³ÌÐò£ºfunction [y] = WFSSY23(n )
%UNTITLED3 Summary of this function goes here % Detailed explanation goes here x=input('please input x:'); if x<0|(fix(x)~=x)
disp('Input parameter must be a positive integer!');
end y=1; for x=1:1:x y=y*x;
end
½á¹û£ºplease input x:3
ans = 6
9
please input x:4 ans =24
4£®ÕÒµ½Ò»¸ö n!> 10100 µÄÖµ£¨ÀûÓÃÉÏÌâµÄn½×³Ëº¯ÊýÎļþ£© ³ÌÐò£ºfunction [y ] = WFSSY24( n )
%UNTITLED4 Summary of this function goes here % Detailed explanation goes here x=input('please input x:'); if x<0|(fix(x)~=x)
disp('Input parameter must be a positive integer!');
end y=1;
for x=1:1:x y=y*x;
end
if y>10^100 y;
else
disp('n!<10^100!');
end
½á¹û£ºplease input x:5
ans =120
please input x:9999 n!>10^100! ans =Inf
5£®ÒÑ֪һάÊý×é A = [2,4,5,8,10]¡¢B =[4,9,6,7,4]£¬ÓÃfor Ñ»·Óï¾äʵÏֺͺ¯Êý¿ÉÓÃsum()
³ÌÐò:function [s,c ] = WFSSY25(i,n )
%UNTITLED2 Summary of this function goes here % Detailed explanation goes here A = [2,4,5,8,10]; B =[4,9,6,7,4];
n=input('please input n:'); fori=1:1:n
c=A(i)*B(n-i+1); end s=sum(c) end
?ABii?1nn?i?1£¬Çó
½á¹û£ºplease input n:3
s = 20
6£®±àдÑé֤ħ·½¾ØÕóµÄº¯ÊýÎļþ£¬Êä³öÒªÇóÈçÏ£º £¨1£©Èç¹ûÊäÈë¾ØÕóµÄάÊýСÓÚ3£¬Êä³öÏÔʾ¡¯error¡¯
10
£¨2£©Èç¹ûÊäÈë¾ØÕóµÄ²»ÊÇ·½Õó£¬Êä³öÏÔʾ¡¯the size of matrix X mustbe N-by-N matrix¡¯ £¨3£©ÏÔʾÐС¢Áкͼ°Æä¶Ô½ÇÏßÇóºÍºóµÄÖµ£¬²¢ÅÐ¶ÏÆäºÍÊÇ·ñÏàͬ¡£Èô²»Í¬£¬ÏÔʾ¡®No¡¯,ÏàͬÏÔʾ¡®Yes¡¯¡£
³ÌÐò: function [ b,m,n,c,d,p,e,f,j,h,i ] = WFSSY26( A ) %UNTITLED3 Summary of this function goes here % Detailed explanation goes here A=input('Enter matrix A=>'); b=ndims(A);
[m,n] = size(A); if b<3
disp('error') ; end if m~=n
disp('the size of matrix X must be N-by-N matrix'); end
c=sum(A,1); d=sum(A,2); p=diag(A);
e=diag(fliplr(A)); f=sum(c)/m; g=sum(d)/n; h=sum(p); i=sum(e); if f==g; if h==i if f==h disp('Yes') else
disp('No') end else
disp('No') end else
disp('No') end
½á¹û£ºEnter matrix A=>[17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9] error
11
Yes ans =2
Enter matrix A=>[17 24 5 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9] error No
ans =2
ʵÑéÈýMATLAB µÄͼÐλæÖÆ
Ò»¡¢ÊµÑéÄ¿µÄ¼°ÒªÇó£º
1.ÕÆÎÕMATLAB »æÍ¼µÄ»ù±¾·½·¨£¬ÊìϤ¸÷ÖÖ»æÍ¼º¯ÊýµÄʹÓã» 2.ÕÆÎÕͼÐεÄÐÞÊη½·¨ºÍ±ê×¢·½·¨£» 3.Á˽âMATLAB ÖÐͼÐδ°¿ÚµÄ²Ù×÷¡£
¶þ¡¢ÊµÑéÄÚÈÝ£º
x=£Û£2¦Ð£¬2¦Ð£Ý£¬y1=sinx¡¢y2=cosx¡¢y3=sin2x¡¢y4=cos 2x
¢ÙÓÃMATLABÓïÑÔ·ÖËĸöÇøÓò·Ö±ð»æÖƵÄÇúÏߣ¬²¢ÇÒ¶ÔͼÐαêÌâ¼°ºá×Ý×ø±êÖá½øÐбê×¢¡£ ³ÌÐò£º
closeall clc
x=[-2*pi:pi/1000:2*pi]; y1=sin(x); y2=cos(x); y3=sin(2*x); y4=cos(2*x); subplot(2,2,1) plot(x,y1);
axis([-2*pi 2*pi -1 1]) set(gca,'xtick',-2*pi:pi:2*pi) set(gca,'ytick',-1:0.5:1)
set(gca,'xticklabel','-2pi|-pi|0|pi|2pi') set(gca,'yticklabel','-1|-0.5|0|0.5|1') xlabel('x');ylabel('y1'); title('sin(x)'); gridon; subplot(2,2,2)
axis([-2*pi 2*pi -1 1])
12