0% found this document useful (0 votes)
299 views

Code Matlab Mon Phuong Phap Tinh Va Matlab

This document contains code in MATLAB for solving equations and performing operations on vectors. It includes: 1. Code to use the Newton's method to find the root of a polynomial equation by taking the derivative and iteratively updating an initial guess. 2. Code to use Gaussian elimination to solve a system of 4 linear equations input as a matrix. 3. A function definition to take in 6 elements as input and output results - finding the maximum, sum, elements divisible by 3, and adding a 9th degree polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
299 views

Code Matlab Mon Phuong Phap Tinh Va Matlab

This document contains code in MATLAB for solving equations and performing operations on vectors. It includes: 1. Code to use the Newton's method to find the root of a polynomial equation by taking the derivative and iteratively updating an initial guess. 2. Code to use Gaussian elimination to solve a system of 4 linear equations input as a matrix. 3. A function definition to take in 6 elements as input and output results - finding the maximum, sum, elements divisible by 3, and adding a 9th degree polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

% Ho va ten: Nguyen Van Luong

% MSSV: 20146959
% De so 2

%Bai3. a) Giai phuong trinh bai 1-c su dung phuong phap Newton
disp('Nhap vao cac he so cua phuong trinh:');
a1=input('He so thu 1:');
a2=input('He so thu 2:');
a3=input('He so thu 3:');
a4=input('He so thu 4:');
% Khoi tao ham so
fx=[a1 a2 a3 a4];
% Dao ham cua f(x)
ffx=polyder(fx);
% Chon x0 thuoc khoang phan ly nghiem
x0=-6;
% Xac dinh nghiem xap xi thu n
n=1;
while n<6
x=x0-polyval(fx,x0)/polyval(ffx,x0);
x0=x;
n=n+1;
end
disp('Nghiem gan dung cua phuong trinh: x=');
disp(x);
%Bai3. a) Giai he bai 2-b su dung Gauss - Jordan
disp('Nhap vao cac he so cua he phuong trinh dang ma tran mo rong (A|b):');
a11=input('a11 ='); a12=input('a12 ='); a13=input('a13 ='); a14=input('a14
=');b1=input('b1 =');
a21=input('a21 ='); a22=input('a22 ='); a23=input('a23 ='); a24=input('a24
=');b2=input('b2 =');
a31=input('a31 ='); a32=input('a32 ='); a33=input('a33 ='); a34=input('a34
=');b3=input('b3 =');
a41=input('a41 ='); a42=input('a42 ='); a43=input('a43 ='); a44=input('a44
=');b4=input('b4 =');
% Khoi tao ma tran
A=[a11 a12 a13 a14 b1;a21 a22 a23 a24 b2;a31 a32 a33 a34 b3;a41 a42 a43 a44
b4];
disp('He phuong trinh viet duoi dang ma tran A|b'); disp(A);
% Tinh kich thuoc A
s=size(A);
% n Xác dinh ma tran vuong gom cac he so cua ve trai he phuong trinh
n=min(s);
for i=1:n
% Dua hàng co he so lon nhat len dau
[t,r]=max(abs(A(i:end,i)));
t=A(r+i-1,:);
A(r+i-1,:)=A(i,:);
A(i,:)=t;
% Chia cac he so cua hàng i cho phan tu [i,i]
A(i,[1:i-1 i+1:end])=A(i,[1:i-1 i+1:end])/A(i,i);
% Dua phan tu [i,i] ve gia tri 1
A(i,i)=1;
% Vector hang s gom cac phan tu cua hàng i tru di phan tu [i,i]
s=A(i,i+1:end);
% Rut gon các hang trong ma tran moi không chua hàng i,cot i theo he so
% cua hàng i
for j=[1:i-1 i+1:n]
A(j,i+1:end)=A(j,i+1:end)-A(j,i).*s;
end
% Dua cac phan tu cot i, ngoài phan tu [i,i] ve 0
A([1:i-1 i+1:end],i)=0;

end

disp('He phuong trinh sau khi bien doi Gauss-Jordan'); disp(A);


disp('Vay nghiem cua he la:');
disp('x1='); disp(A(1,5));
disp('x2='); disp(A(2,5));
disp('x3='); disp(A(3,5));
disp('x4='); disp(A(4,5));
% Ho va ten: Nguyen Van Luong
% MSSV: 20146959
% De so 2
% Bai nay em dung function nên phai luu sang 1 file khac thay a !

%Bai 3. b) Ham nhap vector co chieu dai 6


function result = bai3b(a,b,c,d,e,f)
% INPUT 6 phan tu cua vector
% Tao vector v voi 6 phan tu dau vao
v=[a b c d e f];

% i. Tim gia tri lon nhat cua cac phan tu vector


maxv = max(v);
disp('Phan tu lon nhat cua vector v:'); disp(maxv);

% ii. Tinh tong cac phan tu vector v


tong=0;n=1;
while(n<7)
tong=tong+v(n);
n=n+1;
end
disp('Tong cac phan tu cua vector:'); disp(tong);

% iii. In ra cac phan tu la boi cua 3


disp('Cac phan tu la boi cua 3:')
k=1;
while(k<7)
if mod(v(k),3) == 0; disp(v(k)); end
k=k+1;
end

% iv. coi v la da thuc bac 5, nhap da thuc t bac 9 va tinh tong 2 da thuc
disp('Nhap vao cac he so cua da thuc bac 9');
x1=input('Nhap vao he so thu 1:');
x2=input('Nhap vao he so thu 2:');
x3=input('Nhap vao he so thu 3:');
x4=input('Nhap vao he so thu 4:');
x5=input('Nhap vao he so thu 5:');
x6=input('Nhap vao he so thu 6:');
x7=input('Nhap vao he so thu 7:');
x8=input('Nhap vao he so thu 8:');
x9=input('Nhap vao he so thu 9:');
x10=input('Nhap vao he so thu 10:');
% Khoi tao da thuc
t=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10];
g=abs(length(v) - length(t));
vector0 = zeros(1,g);
v=[vector0 v];
vectortong=v+t;
% Tinh tong hai da thuc
disp('Tong cua 2 da thuc:');
disp(vectortong);
end

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy