Advertisement
Guest User

AE5139HW1

a guest
Aug 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.17 KB | None | 0 0
  1. % AE 5139 Intro to CFD
  2. % Homework 1
  3. % Due: 27 Aug 2019
  4. % Christopher Bates
  5.  
  6. clear all
  7. close all
  8. clc
  9.  
  10. StepSize     = [0.1, 0.01];
  11. x0           = 0;
  12. xmax         = 3;
  13.  
  14. syms x
  15. dydx         = @(x) x^2 + tanh(x);  
  16. y            = @(x) (1/3)*x^3 + log(cosh(x));
  17.  
  18.  
  19. Error = zeros(2);
  20.  
  21. i = 0;
  22.  
  23. for k = StepSize
  24.     i = i+1;
  25.    
  26.    X = x0:StepSize(i):xmax;
  27.    
  28.    [numRows, numElems] = size(X);
  29.    
  30.    y_apx = zeros(numElems);
  31.    
  32.    y_apx(1) = 0 + dydx(X(1)) * StepSize(i);
  33.    
  34.    for j = 2:numElems
  35.        
  36.        y_apx(j) = y_apx(j-1) + dydx(X(j-1)) * StepSize(i);
  37.        
  38.        if j == numElems
  39.            figure
  40.            plot(X,y_apx)
  41.            xlabel('x-axis','Interpreter','latex')
  42.            ylabel('y-axis','Interpreter','latex')
  43.            mytitleText = ['Approximate value of integral for \Deltax = ',num2str(StepSize(i))];
  44.            title(mytitleText,'Interpreter','tex' );
  45.            
  46.            Error(i) = (y(xmax) - y_apx(numElems)) / y(xmax);
  47.        end
  48.    end
  49.    
  50.    if i == 1
  51.        ValueTable = table(X',y_apx);
  52.        ValueTable.Properties.VariableNames = {'x','y'};
  53.        writetable(ValueTable,'ValueTable.txt')
  54.    end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement