letscheat1234

Master Matlab

Nov 9th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 13.30 KB | None | 0 0
  1. 1Basics
  2.  
  3. Example: If A= [3 0 1, 0 1 0, 2 0 0] find (a) Characteristic polynomial of A, (b) Roots of the characteristic polynomial A, (c) eigenvalues of A, (d) eigenvectors of A, (e) find eigenvalues of A -1 , (f) find eigenvalues of A T , (g) find eigenvalues of B=A 2 +3A+2I
  4. A=[3 0 -1; 0 1 0; 2 0 0]
  5. Eigenvalues_of_A=eig(A)
  6. [X,D]= eig(A)
  7. p=poly(A) % Coefficients of the characteristic polynomial
  8. r = roots(p) % To find the roots of the characteristic equation
  9. sum_of_eigenvalues=sum(r)
  10. Trace_of_A=trace(A) %Note that sum of eigenvalues is Trace of A
  11. product_of_eigenvalues_A=prod(r)
  12. Determinant_of_A =det(A) %Note that product of the eigenvalues of A is determinant of A
  13. Inverse_A=inv(A)
  14. Eigenvalues_of_invA= eig(Inverse_A)
  15. Eigenvalues_Transpose_of_A= eig(A') % Note that eigenvalues of A and Transpose of A are same
  16. Eigenvalues_of_B= eig(A^2+3*A+2*eye(3)) % Understand the property it is satisfying
  17.  
  18.  
  19. Aim : Matrix Manipulation in Matlab
  20.  
  21. Q1
  22. a=[1 2 3;4 5 6;7 8 9]
  23. m=size(a)
  24. a(3,2)
  25.  
  26. Q2
  27. b=[1 1 3;1 5 1;3 1 1]
  28. det(b)
  29. rank(b)
  30. trace(b)
  31. diag(b)
  32.  
  33. Q3
  34. b=[1 1 3;1 5 1;3 1 1]
  35. b'
  36. b^2
  37. inv(b)
  38. b^-1
  39.  
  40. Q4
  41. c=a+b
  42. d=a-b
  43. e=a*b
  44. f=a.*b
  45. g=a./b
  46.  
  47. Q5
  48. x=[a b]
  49. y=[a;b]
  50.  
  51. A5
  52.  
  53. x =
  54.  
  55.      1     2     3     1     1     3
  56.      4     5     6     1     5     1
  57.      7     8     9     3     1     1
  58.  
  59.  
  60. y =
  61.  
  62.      1     2     3
  63.      4     5     6
  64.      7     8     9
  65.      1     1     3
  66.      1     5     1
  67.      3     1     1
  68.  
  69.  
  70. Q6
  71. poly(b)
  72. roots(poly(b))
  73. eig(b)
  74. [v d]=eig(b)
  75.  
  76. A6
  77.  
  78. ans =
  79.     1.0000   -7.0000   -0.0000   36.0000
  80.  
  81. ans =
  82.     6.0000
  83.     3.0000
  84.    -2.0000
  85.  
  86. ans =
  87.    -2.0000
  88.     3.0000
  89.     6.0000
  90.  
  91. v =
  92.    -0.7071    0.5774    0.4082
  93.          0   -0.5774    0.8165
  94.     0.7071    0.5774    0.4082
  95.  
  96.  
  97. d =
  98.    -2.0000         0         0
  99.          0    3.0000         0
  100.          0         0    6.0000
  101.  
  102. Q7
  103. x=[1 -1 0 1 2 3 4 5]
  104. y=[0 1 2 3 4 5 6 7]
  105. x.*y
  106. max(x)
  107. min(y)
  108.  
  109. Q8
  110. a=1:5
  111. a=0:2:10
  112. a=linspace(1,5)
  113. a=linspace(1,5,10)
  114.  
  115. Q9
  116. ones(3)
  117. ones(2,3)
  118. zeros(3,3)
  119. eye(2)
  120.  
  121. Q10
  122. a=[4 -2 6;2 8 2;6 10 3]
  123. b=[8;4;0]
  124. inv(a)*b
  125.  
  126. Q11
  127. a=[3 0 -1;0 1 0;2 0 0]
  128. poly(a)
  129. eig(a)
  130. [v d]=eig(a)
  131. eig(a^-1)
  132. eig(a')
  133. b=a^2+3*a+2*eye(3)
  134. eig(b)
  135.  
  136.  
  137. 2Graph
  138.  
  139. Axis
  140. >> x=[0:0.01:10];
  141. >> y=exp(-x).*sin(2*x + 3);
  142. >> plot(x,y),axis([0 10 -1 1])
  143.  
  144. Title and Label -
  145. >> x=linspace(0,2*pi,100);
  146. >> y=sin(x);
  147. >> plot(x,y);
  148. >> title('this is sinus function');
  149. >> xlabel('x-values');
  150. >> ylabel('y-values');
  151.  
  152. Multi Plot
  153. >> x=[0:0.01:10];
  154. >> y=sin(x);
  155. >> g=cos(x);
  156. >> plot(x,y,x,g,'.-'), legend('sin(x)','cos(x)')
  157.  
  158. Grid
  159. >> t=0:pi/200:2*pi;
  160. >> y1=sin(t);
  161. >> y2=sin(t+pi/2);
  162. >> plot(t,y1,t,y2);
  163. >> grid on
  164.  
  165. Subplot 1
  166. >> t=0:pi/200:2*pi;
  167. >> y1=sin(t);
  168. >> y2=sin(t+pi/2);
  169. >> subplot(2,2,1)
  170. >> plot(t,y1)
  171. >> subplot(2,2,2)
  172. >> plot(t,y2)
  173.  
  174. Subplot 2
  175. >> x=[0:0.1:10];
  176. >> f=figure;
  177. >> f1=subplot(1,2,1);
  178. >> plot(x,cos(x),'r');
  179. >> grid on;
  180. >> title('cosine');
  181. >> f2=subplot(1,2,2);
  182. >> plot(x,sin(x),'d');
  183. >> grid on;
  184. >> title('sine');
  185.  
  186. Polynomial Function
  187. >> s=linspace(-5,5,100);
  188. >> coeff = [1 3 3 1];
  189. >> A = polyval(coeff,s);
  190. >> plot(s,A)
  191. >> title('Polynomial Function');
  192. >> grid on;
  193.  
  194. 3Caley Hamilton
  195.  
  196. clear all
  197. clc
  198. A=input('Enter the matrix A:');
  199. n=length(A);
  200. c=poly(A) % Coefficients of the characteristic polynomial
  201. P=zeros(n,n); % Initialization purpose
  202. for i=1:n+1
  203.  P=P+c(i)*A^(n+1-i) % Characteristic equation
  204. end
  205. Determinant=det(A)
  206. if det(A)~= 0
  207. inv_A=zeros(n,n)
  208. for j=1:n
  209.  inv_A= inv_A - ((1/c(n+1))*(c(j)*A^(n-j)))
  210. end
  211. else
  212.  disp( 'A is a singular matrix')
  213. end
  214.  
  215. Input : A:[3 0 -1; 0 1 0; 2 0 0]
  216.  
  217.  
  218. 4Diagonalization
  219. clear all;
  220. clc;
  221. A=input('Enter the matrix A:');
  222. n=length(A);
  223. [X,D]=eig(A);
  224. fprintf('\n%s','The eigen values of A are')
  225. disp(diag(real(D))')
  226. disp('The eigen vectors for the corresponding eigenvalues')
  227. disp(X)
  228. option=input('If you want to diagonalize by similarity transformation then
  229. press 1 or press any number:');
  230. if(option==1)
  231.  %Diagonalization of the real matrix A having distinct eigenvalues by
  232.  %similarity transformation
  233.  disp('Modal matrix associated with A is')
  234.  P=X
  235.  disp('D=inv(P)*A*P')
  236.  D=inv(P)*A*P
  237.  disp('Thus A is reduced to the diagonal matrix D through P by similarity
  238. transformation.')
  239. else
  240. % Diagonalization of the real symmetric matrix A having distinct eigenvalues
  241. by orthogonal transformation
  242. for i=1:n,
  243.  x=X(:,i);
  244.  u(:,i)=x/norm(x);
  245. end
  246. disp('Orthogonal matrix associated with A is the matrix')
  247. P1=u(:,1:n);
  248. disp('D=transpose(P1)*A*P1')
  249. D=(P1)'*A*(P1)
  250. disp('Thus A is reduced to the diagonal matrix D through P1 by orthogonal
  251. transformation.')
  252. end
  253.  
  254.  
  255. Output:
  256. Enter the matrix A:[-1 2 -2;1 2 1;-1 -1 0]
  257.  
  258.  
  259. 5SOLUTION OF SYSTEM OF SECOND ORDER DIFFERENTIAL EQUATIONS OF THE FORM Y + AY = 0
  260.  
  261. clear all
  262. clc
  263. syms t a b
  264. A = [1 2;2 -1];
  265. l=eig(A);
  266. [p,q]=eig(A);
  267. s1 = dsolve('D2x+a*x=0','t')
  268. s2 = dsolve('D2x+b*x=0','t')
  269. disp(s1)
  270. disp(s2)
  271. disp('The Solution of the given system');
  272. y = p*[s1;s2]
  273. soln = subs(y,{a,b},{q(1),q(4)})
  274.  
  275. 6SERIES SOLUTIONS
  276.  
  277. clear all
  278. clc
  279. syms x p n
  280. n = input('Enter the number of terms up to which you want to evaluate the series (between 1 and 10) : ')
  281. n=n-1;
  282. disp('Note : Input the coefficients of DE as constant function of x');
  283. p(1)= input('The coefficient of D2y : ');
  284. p(2)= input('The coefficient of Dy : ');
  285. p(3)= input('The coefficient of y : ');
  286.  
  287. for i = 0:n
  288.     tmp = strcat('a', num2str(i));
  289.     a(i+1) = sym(tmp);
  290. end
  291.  
  292. y=sum(a.*x.^(0:n));
  293. ps = collect(p(1)*diff(y,2) + p(2)*diff(y) + p(3)*y ,x);
  294. psc=coeffs(ps,x);
  295. i1=input('Enter the first IV: ');
  296. i2=input('Enter the second IV: ');
  297. ics1=strcat('a0=',num2str(i1));
  298. ics2=strcat('a1=',num2str(i2));
  299. sol=vpa(subs(y,solve(ics1,ics2,psc(1:(n-1)))),3);
  300. disp('The solution of the given DE is:');
  301. disp(sol);
  302. ezplot(sol,[-4,4]);
  303.  
  304. 7Bessels
  305.  
  306. Aim – Bessels Equation of first kind
  307.  
  308. X = 0:0.1:20;
  309. J = zeros(5,nume1(X));
  310. for i=0 : 4
  311.     J(i+1,:) = besselj(i,X);
  312. end
  313. plot (X,J,'LineWidth',1.5)
  314. grid on;
  315. legend('J_0','J_1','J_2','J_3','J_4','Location','Best');
  316. title('Bessel Functions of the First Kind for n=0,1,2,3,4')
  317. xlabel('x')
  318. ylabel('J_n(X)')
  319.  
  320.  
  321.  
  322. Aim – Bessels Equation of second kind
  323.  
  324. clc
  325. clear all
  326. X = 0:0.1:20;
  327. Y = zeros(5,numel(X));
  328. for i=0 : 4
  329.     Y(i+1,:) = bessely(i,X);
  330. end
  331. plot (X,Y,'LineWidth',1.5)
  332. axis([-0.1 20.2 -2 0.6]);
  333. grid on;
  334. legend('Y_0','Y_1','Y_2','Y_3','Y_4','Location','Best');
  335. title('Bessel Functions of the First Kind for n=0,1,2,3,4')
  336. xlabel('X')
  337. ylabel('Y_n(X)')
  338. 8Fourier
  339.  
  340. clear all
  341. clc
  342. syms x k L U n
  343. f = input('Enter the functions: ');
  344. L = input('enter the lower limit: ');
  345. U = input('Enter the upper limit: ');
  346. l = (U-L)/2;
  347. n = input('Enter the no. of terms required: ');
  348. ak = @(f,x,k) int(f*cos(k*pi*x/l)/l,x,L,U);
  349. bk = @(f,x,k) int(f*sin(k*pi*x/l)/l,x,L,U);
  350. fs = @(f,x,n) ak(f,x,0)/2 + symsum(ak(f,x,k)*cos(k*pi*x/l) + bk(f,x,k)*sin(k*pi*x/l),k,l,n);
  351. fst = ak(f,x,0)/2;
  352. for i=1:n
  353.    fst = fst + ak(f,x,i)*cos(i*pi*x/l) + bk(f,x,i)*sin(i*pi*x/l);
  354.    disp(['Harmonics upto:', num2str(i) ]);
  355.    disp(fst);
  356.   ezplot(f,[L U]);
  357.    hold on;
  358.   ezplot(fst,[L U]);
  359.    %set(h,'LineStyle','-','color',[i/n 1/i 1/n],'LineWidth',2);
  360.    title(['Parallel sums up to n=',num2str(i) ])
  361. end
  362.  
  363. 9Legandre
  364.  
  365. X = linspace(-1,1,500); X = X(:);
  366. N = 10;
  367. Y = zeros(numel(X),N);
  368. Y(:,1) = 1;
  369. Y(:,2) = X;
  370. for n=1:(N-1)
  371.  Y(:,n+2) = ((2*n+1) .* X .* Y(:,n+1) - n .* Y(:,n)) / (n+1);
  372. end
  373. figure('Position',[0 0 1024 768])
  374. 19
  375. plot(X, Y(:,1:6), 'LineWidth', 1.5)
  376. axis([-1 1 -1.1 1.1])
  377. grid on;
  378. legend(num2str((1:6)'-1,'P_%d(x)'),'Location','Best')
  379. xlabel('x')
  380. ylabel('P_n(x)')
  381. title('Legendre Polynomials')
  382.  
  383. 10. HARMONIC ANALYSIS
  384.  
  385. Aim: From the given data points of a function , generate the first few harmonics of Fourier series and visualize them () f x
  386. MATLAB Code:
  387. clear all
  388. clc
  389. syms t
  390. n = input('Enter the number of data points n : ');
  391. x_0 = input('Enter the starting value of x : ');
  392. s = input('Enter the length of the spacing between successive values of x : ');
  393. count = input('Enter 0 if the unit of x is degrees, otherwise enter a non-zero number: ');
  394. y = input('Enter the y values (as a row vector): ');
  395. n1 = input('Enter the number of harmonic of the series n1 : ');
  396. x = x_0 + (0:n-1)*s; if(count == 0)
  397. x=x*pi/180;
  398. s=s*pi/180;
  399. end
  400. l=0.5*(x(n)+s-x(1)); % l=pi if it is degree
  401. a_0 = (2/n)*sum(y);
  402. for i=1:n1
  403. yc=y.*cos(i*pi*x/l);
  404. ys=y.*sin(i*pi*x/l);
  405. a(i)=(2/n)*sum(yc);
  406. b(i)=(2/n)*sum(ys);
  407. end
  408. F_s=a_0/2;
  409. figure('Position',[1 1 1024 768])
  410. for i=1:n1
  411. subplot(n1,1,i)
  412. plot(x,y,'r*');
  413. hold on
  414. F_s = F_s+a(i).*cos(i*pi*t/l)+b(i).*sin(i*pi*t/l);
  415. ezplot(F_s, [x(1) x(n)])
  416. title(['Harmonics up to: ', num2str(i)])
  417. end
  418. disp('Fourier series : ')
  419. vpa(F_s,4)
  420.  
  421. 11. SOLUTION FOR SECOND ORDER DIFFERENCE EQUATIONS WITH CONSTANT COEFFICIENTS
  422.  
  423. Aim: To write the MATLAB code to find the solution for the second order linear difference equation with constant co-efficient.
  424.  
  425. MATLAB Code
  426. clear all
  427. clc
  428. syms n k1 k2
  429. a = input('Enter the coefficient of y(n+2): ');
  430. b = input('Enter the coefficient of y(n+1): ');
  431. c = input('Enter the coefficient of y(n): ');
  432. g = input('Enter the non-homogeneous part: ');
  433. r = roots([a b c]);
  434. if imag(r)~=0
  435. rho = sqrt(real(r(1))^2 + imag(r(1))^2);
  436. theta = atan(abs(imag(r(1)))/real(r(1)));
  437. y1 = (rho^n)*cos(n*theta);
  438. y2 = (rho^n)*sin(n*theta);
  439. elseif r(1)==r(2)
  440. y1 = r(1)^n;
  441. y2 = n*r(1)^n;
  442. else
  443. y1 = r(1)^n;
  444. y2 = r(2)^n;
  445. end
  446. Co = det([y1, y2;subs(y1,n,n+1), subs(y2,n,n+1)]); %Casoratian of the solutions
  447. y_c = k1*y1 + k2*y2;
  448. disp('Complementary Solution is: ');
  449. disp(y_c);
  450. if(g ~= 0)
  451. y11 = subs(y1,n,n+1);
  452. y21 = subs(y2,n,n+1);
  453. Co1 = subs(Co,n,n+1);
  454. u1 = simplify(symsum(-g*y21/Co1,n,0,n-1));
  455. u2 = simplify(symsum(g*y11/Co1,n,0,n-1));
  456. y_p = simplify(u1*y1+u2*y2);
  457. y = y_c + y_p;
  458. else
  459. y = y_c;
  460. end
  461. check = input('If the given problem has initial conditions then enter 1 else enter 0: ');
  462. if (check == 1)
  463. yval1 = input('Enter the initial condition at n = 0: ');
  464. yval2 = input('Enter the initial condition at n = 1: ');
  465. cond1 = strcat(char(subs(y,n,0)),'=',num2str(yval1));
  466. cond2 = strcat(char(subs(y,n,1)),'=',num2str(yval2));
  467. [k1,k2] = solve(cond1,cond2);
  468. y = subs(y);
  469. end
  470. disp('Complete Solution is: ')
  471. disp(collect(collect(y,y1),y2))
  472. if(check ~= 0)
  473. nrange = linspace(0,10,40);
  474. Y = subs(y,n,nrange);
  475. stem(nrange,Y);
  476. set(gca,'XTick',linspace(0,10,11))
  477. xlabel('n');
  478. ylabel('y(n)');
  479. end
  480.    
  481.  
  482. 12. SOLUTION OF FIRST AND SECOND ORDER DIFFERENCE EQUATIONS USING Z - TRANSFORM
  483.  
  484. Aim: To solve and visualize Difference equation using Z – Transform in MATLAB.
  485. MATLAB CODE for solving second order difference Equations:
  486. clc
  487. clear all
  488. syms n z Yz g
  489. yn = sym('y(n)');
  490. yn1 = sym('y(n+1)');
  491. yn2 = sym('y(n+2)');
  492. a = input('The Coefficient of y(n+2) = ');
  493. b = input('The Coefficient of y(n+1) = ');
  494. c = input('The Coefficient of y(n) = ');
  495. nh = input('The Non Homogeneous part ');
  496. eq = a*yn2 + b*yn1 + c*yn - nh;
  497. Zeq = ztrans(eq, n, z);
  498. if (a==0)
  499. d = input('The initial value at 0 is ');
  500. Zeq = subs(Zeq,{'ztrans(y(n), n, z)', 'y(0)'}, {Yz, d});
  501. else
  502. d = input('The initial value at 0 is ');
  503. e = input('The initial value at 1 is ');
  504. Zeq = subs(Zeq,{'ztrans(y(n), n, z)', 'y(0)', 'y(1)'}, {Yz, d, e});
  505. end
  506. eq = collect(Zeq, Yz);
  507. Y = solve(eq, Yz);
  508. Y = iztrans(Y, z, n);
  509. Y = simple(Y)
  510. m = 0:20;
  511. y = double(subs(Y,n,m));
  512. stem(y)
  513. title('Difference equation');
  514. xlabel('n'); ylabel('y(n)');
  515.  
  516. 13. APPLICATIONS OF SECOND ORDER DIFFERENTIAL EQUATIONS (Springs)
  517.  
  518. AIM : To know how the second order differential equations can be applied to solve problems concerning the vibrations of springs using MATLAB.
  519.  
  520. MATLAB Code:
  521. clear
  522. clc
  523. syms x k1 k2
  524. a=input('Enter the coefficient of D2y:');
  525. b=input('Enter the coefficient of Dy:');
  526. c=input('Enter the coefficient of y:');
  527. a1=a/a; b1=b/a; c1=c/a;
  528. eq='a1*x^2+b1*x+c1=0'; %%% solving of homogeneous part
  529. eq1=subs(eq,'[a1,b1,c1]',[a1,b1,c1]);
  530. r=solve(eq1, 'x');
  531. if image(r)~=0
  532. disp('under damping:')
  533. y1=exp(real(r(1))*x)*cos(imag(r(1))*x);
  534. y2=exp(real(r(1))*x)*sin(abs(imag(r(1)))*x);
  535. elseif r(1)==r(2)
  536. disp('critical damping:')
  537. y1=exp(r(1)*x);
  538. y2=x*exp(r(1)*x);
  539. else
  540. disp('over damping:')
  541. y1=exp(r(1)*x);
  542. y2=exp(r(2)*x);
  543. end
  544. W=simplify(y1*diff(y2)-y2*diff(y1)); %%W is the Wronskian
  545. y_c=k1*y1+k2*y2;
  546. disp('The complementary sol y_c is:')
  547. g=input('Enter the non-homogeneous part:'); %%% Solving of Non-homogeneous
  548. y_p=(1/a)*(-y1*int(y2*g/W)+y2*int(y1*g/W));
  549. y=y_c + y_p;
  550. disp('The particular sol y_p is:')
  551. disp(y_p)
  552. disp('hence the general sol y is:')
  553. disp(y)
  554. i_1=input('The initial conditions x and the value of y at x in vector form:')
  555. i_2=input('The initial conditions x and Dy at x in vector form:')
  556. i1=strcat(char(subs(y,x,i_1(1))),'=',num2str(i_1(2)));
  557. i2=strcat(char(subs(diff(y),x,i_2(1))),'=',num2str(i_2(2)));
  558. [k1, k2]=solve(i1,i2);
  559. C_S=vpa(subs(y),3);
  560. disp('Required complete solution is:')
  561. disp(C_S)
  562. ezplot(C_S,[0 1.5])
  563.  
  564.  
  565. 14. APPLICATIONS OF SECOND ORDER DIFFERENTIAL EQUATIONS (Electric Circuits)
  566.  
  567. AIM
  568. To know how the second order differential equations can be applied to solve problems concerning the analysis of electric circuits using MATLAB
  569.  
  570. Same Code as previous
  571.  
  572. MATLAB Output
  573. In the Command Window:
  574. Enter the coefficient of D2Q: 1
  575. Enter the coefficient of DQ: 40
  576. Enter the coefficient of Q: 625
Add Comment
Please, Sign In to add comment