Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.91 KB | None | 0 0
  1. %% 3 Finite element method with Gauss quadrature
  2.  
  3. figure();
  4. hold('on');
  5.  
  6. for n=[10,20,40,80,160]
  7.     L=10;
  8.     h=L/n;
  9. %why is this this way? (the two last lines are extra)
  10.     A=2/h*diag(ones(n-1,1),0)...
  11.         -1/h*diag(ones(n-2,1),-1)...
  12.         -1/h*diag(ones(n-2,1),1)...
  13.         -1/2*diag(ones(n-2,1),-1)...
  14.         +1/2*diag(ones(n-2,1),1);
  15.  
  16.     b=zeros(n-1,1);
  17.     f=@(x) 5*exp(x);
  18.     phi_l=@(z) (1+z)/2;
  19.     phi_r=@(z) (1-z)/2;
  20.     for i=1:n-1
  21.         z2x_l=@(z) (i-1/2)*h+z*h/2;
  22.         g_l=@(z) phi_l(z)*f(z2x_l(z));
  23.         z2x_r=@(z) (i+1/2)*h+z*h/2;
  24.         g_r=@(z) phi_r(z)*f(z2x_r(z));
  25.         b(i)=h/2*(1*g_l(1/sqrt(3))+1*g_l(-1/sqrt(3)))+h/2*(1*g_r(1/sqrt(3))+1*g_r(-1/sqrt(3)));
  26.     end
  27.  
  28.     u=[0;A\b;0];
  29.     x=0:h:L;
  30.     plot(x,u,'k-');
  31. end
  32.  
  33. ua=-exp(x).*(5*x-(5*(1+9*exp(10)))/(-1+exp(10))-5)-50*exp(10)/(-1+exp(10));
  34. plot(x,ua,'r-');
  35.  
  36. hold('off');
  37. xlabel('x');
  38. ylabel('u');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement