Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. %Subject: Problem Set #16
  2. %% Problem 1
  3. clear all clc
  4. A = [1, 3, -5, 9, -1; 2, -2, -3, -7, 2; 1, -5, 2, -16, 3]; B = [5; 5; 0];
  5. A_a = [A B];
  6. sol = rref(A_a)
  7. sol_symb = sym(sol)
  8. disp('There are infinite many roots')
  9. %% Problem 3
  10. clc, format longG
  11. x_int = [0,1];
  12. y0 = [1;6];
  13. sol = ode45(@syst_prob, x_int, y0);
  14. x = 0:0.01:1;
  15. y = deval(sol, x);
  16. plot(x,y(1,:), 'r', 'LineWidth', 3)
  17. legend('y(x)')
  18. xlabel('x'), ylabel('y(x)')
  19. figure
  20. plot(x, y(1,:), 'r', x,y(2,:), 'g', 'LineWidth', 3)
  21. legend('First order derivative','Second order derivative')
  22. xlabel('x'), ylabel('yprime')
  23.  
  24. function dydx = syst_prob(x,y)
  25. dydx = zeros(2,1);
  26. dydx(1) = y(2);
  27. dydx(2) = 4*x*y(2)-6*y(1)/1+x^2;
  28. end
  29.  
  30. %% Problem 4
  31. clc, clear all,close all
  32. format compact, format longG
  33. izt=@(a,x)(exp(a*(x.^1/2)))/4 + sin(x);
  34. for a=1:10
  35. fun=@(x)izt(a,x);
  36. int_value(a)=integral(fun,0,1) - 2;
  37. end
  38. a_val=1:10;
  39. plot(a_val,int_value,'r','LineWidth',3)
  40. grid
  41. xlabel('a'),ylabel('f(a)')
  42. a_p=[5.8 5.9]; iter=6;
  43. for i=1:2
  44. fun=@(x)izt(a_p(i),x); f(i)=integral(fun,0,1) - 2;
  45. end
  46. for k=1:iter
  47. k1=k+1; i=k+2;
  48. a_p(i)=a_p(k1)-f(k1)*(a_p(k1)-a_p(k))/(f(k1)-f(k));
  49. fun=@(x)izt(a_p(i),x); f(i)=integral(fun,0,1)-2;
  50. M_pr(k,1)=a_p(i);M_pr(k,2)=f(i);
  51. end
  52. disp('approximation to the root:'), disp(a_p(1:2))
  53. disp(' a f(x)'), disp(M_pr)
  54. disp('The value of the root is: 5.9107')
  55. %% Problem 6
  56. clear all, clc, format compact
  57. A = [15, 10, 9; 10, 12, 10; 9, 10, 12]; B = [5; 21; 2];
  58. xapp = zeros(3,1);
  59. iter = 0;
  60. itermax = 60;
  61. k = 1;
  62. while iter < itermax
  63. k = k+1; iter = iter+1;
  64. for i = 1:3
  65. rez = 0;
  66. for j = 1:3
  67. if j~=i
  68. rez=rez+xapp(j, k-1)*A(i,j);
  69. end
  70. end
  71. xapp(i,k) = (B(i,1)-rez)/A(i,i);
  72. end
  73. end
  74. x_approx = xapp(:, k)
  75. x_sol = linsolve(A,B)
  76.  
  77. %% Problem 5
  78. clear all, clc
  79. A = [7, 7, 9; 7, 16, -7; 9, -7, 107]; B = [19;37;4];
  80. ni = 1;
  81. for i = 1:3
  82. if det(A(1:i, 1:i))>0
  83. else ni = 2; break
  84. end
  85. end
  86. if ni == 2
  87. disp('the coefficient matrix is not positive definite'), return
  88. end
  89. check=isequal(A,A');
  90. if check == 0
  91. disp('the coefficient matrix is not symmetric'), return
  92. end
  93. disp('A is symmetric and positive definite ')
  94.  
  95. k_iter = 0; epsi = 10^(-3); itermax = 300; x = [0 2 0];
  96. r = A*x-B; norm_r = norm(r);
  97. while norm_r > epsi & k_iter < itermax
  98. k_iter=k_iter+1;
  99. tau=((A*r)'*r)/norm(A*r)^2;
  100. x=x-(tau*r')';r=A*x-B; norm_r=norm(r);
  101. end
  102. k_iter,tau,x,norm_r
  103. x_sol=linsolve(A,B)
  104. disp('Answer:'),
  105.  
  106. %% Problem 2
  107. clear all, close all, clc
  108. graph1 = ezplot('x1.^3+x2.^3-5',[-1 2 0 3]) %assume that x=x1 & y=x2
  109. hold on
  110. graph2 = ezplot('sin(x2+0.8)-x1-0.7',[-1 2 0 3])
  111. set(graph1,'Color','r','LineWidth',3)
  112. set(graph2,'Color','b','LineWidth',3)
  113. hold off
  114. grid
  115.  
  116. syms x1 x2
  117. xapp=[-0.8 1.70]; xapp_pr=xapp; xpr=[x1 x2];
  118. fun=[x1.^3+x2.^3-5,sin(x2+0.8)-x1-0.7]; % f =(f1 f2)
  119. fun_pr=[diff(fun(1),xpr(1)) diff(fun(1),xpr(2))
  120. diff(fun(2),xpr(1)) diff(fun(2),xpr(2))]% partial derivatives
  121.  
  122. epsi=10^(-5); k=1; % number of iterations
  123. norm_sol=1;
  124. while norm_sol > epsi
  125. for i=1:2
  126. B(i,1)=-double(subs(fun(i),xpr,xapp));
  127. for j=1:2
  128. A(i,j)=double(subs(fun_pr(i,j),xpr,xapp));
  129. end
  130. end
  131. xdelta=A\B; xapp=xapp+xdelta';
  132. c=double(subs(fun,xpr,xapp)); norm_sol=norm(c);
  133. M_pr(k,1)=xapp(1); M_pr(k,2)=xapp(2); M_pr(k,3)=norm_sol; k=k+1;
  134. end
  135.  
  136. disp('approximation to the root:'), disp(xapp_pr(1:2))
  137. disp(' x1 x2 error norm')
  138. disp(M_pr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement