Bài tập sử dụng if for while trong matlab năm 2024
An expression can include relational operators (such as Within the conditional expression of a x = 42; while exist('myfunction.m','file') && (myfunction(x) >= pi) endThe first part of the expression evaluates to false. Therefore, MATLAB does not need to evaluate the second part of the expression, which would result in an undefined function error. The_Gunners Giải bài tập matlab Có mấy bài ko có input , output thì tự viết, tốn giấy vãi Câu 1. a) Tạo hàm để xác định một số n có phải là số nguyên tố hay không functionf=songto(x)%input x%output f=1 thi x la so nguyen to, f=0 thi x khong phai so nguyen tos=0;fori=1:x/2 ifmod(x,i)==0;s=s+1; endendifs==1f=1;elsef=0;
s=0;i=0;whiles<10i=i+1; ifsongto(i)==1;s=s+1;disp(i); end;end; Câu 2.
functionf=sohoanhao(n)%input n%output f=1 thi x la so hoan hao, f=0 thi x khong phai so hoan haos=0;fori=1:n/2 if(mod(n,i)==0)s=s+i; endendif(s==n)f=1;elsef=0;end;
n=input('Nhap n= ');fori=1:n-1 ifsohoanhao(i)==1disp(i); end;end; Có thể in sao dưới mọi hình thức.^^Khi in sao thì nhớ ghi rõ nguồn nhá1 The_Gunners Câu 3.
functionf=giaithua(n)% ham tinh giai thua cua 1 sof=prod(1:n);
nk \= n!/(k!*(n-k)!) với n, k được nhập từ bàn phím, k<=n %input n,k (n>=k)%output to hop chap k cua nn=input('Nhap n= ');k=input('Nhap k= ');c=giaithua(n)/(giaithua(k)*giaithua(n-k));disp(['To hop chap 'num2str(k)'cua 'num2str(n)' bang: ']); disp(c); Câu 4. Viết 1 script để thể hiện %Nhap ma tranm=input('Nhap so hang cua ma tran: ');n=input('Nhap so cot cua ma tran: ');fori=1:m forj=1:na(i,j)=input(['Nhap a('num2str(i)','num2str(j)')= ']); end;end;disp(a);% Gan cho vector x là dong cuoi cung cua A.x=a(end,:);disp('Dua ra dong cuoi cung cua a: ');disp(x);% Gan cho ma tran y la hai cot dau tien cua A.y=a(1:m,1:2);disp('Dua ra 2 cot dau tien cua a: ');disp(y);% Tinh tong theo dong ma tran A, tinh tong theo cot ma tran Afori=1:ms(i)=0;end;forj=1:nc(j)=0;end;fori=1:m forj=1:ns(i)=s(i)+a(i,j); end;end;fori=1:mdisp(['Tong cac phan tu cua hang 'num2str(i)' = 'num2str(s(i))]); end;forj=1:n fori=1:mc(j)=c(j)+a(i,j); Có thể in sao dưới mọi hình thức.^^Khi in sao thì nhớ ghi rõ nguồn nhá2 The_Gunners end;end;forj=1:ndisp(['Tong cac phan tu cua cot 'num2str(j)'= 'num2str(c(j))]); end;% Tim gia tri lon nhat và nho nhat cua ma tran.min=a(1,1);max=a(1,1);fori=1:m forj=1:n if(maxa(i,j)) min= a(i,j); end; end;end;disp(['Max= 'num2str(max)]);disp(['Min= 'num2str(min)]);% Tinh tong trung binh cac phan tu cua Atb=0;fori=1:m forj=1:ntb=tb+a(i,j); end;end;tb=tb/(m*n);disp(['Tong trung binh cua MT la: 'num2str(tb)]); Câu 5. - Tạo hàm để xác định một số Fibonaxi của số n (F(1)=F(2)=1; F(n)=F(n-2)+F(n-1)) functionf=fibo(x)ifx<=2f=1;elsef=fibo(x-1)+fibo(x-2);end; - Tạo script m-file in ra màn hình những số Fibonaxi nhỏ hơn n (có sử dụng hàm đãtạo), với n được nhập từ bàn phím n=input('Nhap n= ');fori=1:n iffibo(i) Câu 6. - Tạo script, sử dụng vòng lặp for, tính tổngs = 2002 + 2 + 4 + 6 + … + 2*n, với n nguyên dương nhập vào từ bàn phím n=input('Nhap n= ');s=2002;fori=1:n Có thể in sao dưới mọi hình thức.^^Khi in sao thì nhớ ghi rõ nguồn nhá3 |