Unformatted text preview:

Table of ContentsProblem 1. PlottingProblem 1. Bisection methodProblem 1. Newton-Raphson methodProblem 2. Gauss-Seidel MethodProblem 2. Gauss EliminationProblem 3. RegressionProblem 4. InterpolationMethod of Linear EquationsTable of ContentsProblem 1. Plotting ............................................................................................................. 1Problem 1. Bisection method ................................................................................................ 2Problem 1. Newton-Raphson method ...................................................................................... 3Problem 2. Gauss-Seidel Method ........................................................................................... 5Problem 2. Gauss Elimination ............................................................................................... 6Problem 3. Regression ......................................................................................................... 7Problem 4. Interpolation ....................................................................................................... 9Method of Linear Equations ................................................................................................ 11Problem 1. Plottingclearclcv = 500;u = 2400;mo = (2.8)*(10^6);q = (1.4)*(10^4);g = 9.81;t = linspace(0,100);F = u*log(mo./(mo-q*t))-g*t-v;plot(t,F)xlabel('t'),ylabel('F')title('F = u*log(mo/(mo-q*t))-g*t-v')grid on1Problem 1. Bisection methodclearclcv = 500;u = 2400;mo = (2.8)*(10^6);q = (1.4)*(10^4);g = 9.81;F = @ (t) u*log(mo/(mo-q*t))-g*t-v;a = 80; b = 90; imax = 30; tol = 0.000001;Fa = F(a); Fb = F(b);if Fa*Fb > 0 disp('Error:The function has the same sign at points a and b. ') elsedisp('iteration a b (tR) Solution f(tR) Tolerance')for i = 1:imax tR = (a + b)/2; toli = (b - a)/2; FtR = F(tR); fprintf('%3i %11.6f %11.6f %11.6f %11.6f %11.6f\n', i, a, b, tR , FtR , toli) if FtR == 0 fprintf ('An exact solution t =%11.6f was found' ,tR)2break end if toli < tol break end if i == imax fprintf('Solution was not obtained in %i iterations',imax) break end if F(a)*FtR < 0 b = tR; else a = tR; endendenditeration a b (tR) Solution f(tR) Tolerance 1 80.000000 90.000000 85.000000 -5.725428 5.000000 2 85.000000 90.000000 87.500000 22.498948 2.500000 3 85.000000 87.500000 86.250000 8.241841 1.250000 4 85.000000 86.250000 85.625000 1.222373 0.625000 5 85.000000 85.625000 85.312500 -2.260437 0.312500 6 85.312500 85.625000 85.468750 -0.521265 0.156250 7 85.468750 85.625000 85.546875 0.349995 0.078125 8 85.468750 85.546875 85.507812 -0.085775 0.039062 9 85.507812 85.546875 85.527344 0.132075 0.019531 10 85.507812 85.527344 85.517578 0.023141 0.009766 11 85.507812 85.517578 85.512695 -0.031319 0.004883 12 85.512695 85.517578 85.515137 -0.004090 0.002441 13 85.515137 85.517578 85.516357 0.009526 0.001221 14 85.515137 85.516357 85.515747 0.002718 0.000610 15 85.515137 85.515747 85.515442 -0.000686 0.000305 16 85.515442 85.515747 85.515594 0.001016 0.000153 17 85.515442 85.515594 85.515518 0.000165 0.000076 18 85.515442 85.515518 85.515480 -0.000260 0.000038 19 85.515480 85.515518 85.515499 -0.000048 0.000019 20 85.515499 85.515518 85.515509 0.000059 0.000010 21 85.515499 85.515509 85.515504 0.000006 0.000005 22 85.515499 85.515504 85.515501 -0.000021 0.000002 23 85.515501 85.515504 85.515503 -0.000008 0.000001 24 85.515503 85.515504 85.515503 -0.000001 0.000001Problem 1. Newton-Raphson methodclearclcsyms tclcv = 500;u = 2400;mo = (2.8)*(10^6);q = (1.4)*(10^4);3g = 9.81;f = u*log(mo/(mo-q*t))-g*t-v;fprime = diff(f,t);newf = matlabFunction(f);newfprime = matlabFunction(fprime);ti = 80;i = 0;ea = 100;error = 1;maxiter = 50;Div = 10^(-14);Tol = 1e-8;fprintf('i ti f(ti) f''(ti) ea error\n')fprintf('%1.1d %11.6f %11.6f %11.6f %11.6f %11.8f\n',i,ti,newf(ti),newfprime(ti),ea,error)while ea > Tol if abs(newfprime(ti)) < Div fprintf('WARNING: Derivative of f is close to zero, chose a different starting value\n') break elseif i > maxiter fprintf('Maximum number of iterations reached\n') break else told = ti; ti = ti - newf(ti)/newfprime(ti); ea = abs((ti-told)/ti)*100; error = abs(ti-told); i = i + 1; fprintf('%.0d %11.6f %11.6f %11.6f %11.6f %11.8f\n',i,ti,newf(ti),newfprime(ti),ea,error) endendfprintf(['Stopped after ',num2str(i),' iterations\n'])i ti f(ti) f'(ti) ea error0 80.000000 -58.818503 10.190000 100.000000 1.000000001 85.772179 2.868881 11.200643 6.729663 5.772178902 85.516044 0.006025 11.153636 0.299517 0.256135373 85.515503 0.000000 11.153537 0.000632 0.000540154 85.515503 0.000000 11.153537 0.000000 0.00000000Stopped after 4 iterations4Problem 2. Gauss-Seidel Methodclearclcclose allA = [10 2 -1; 2 6 1; 1 3 5];b = [89; 7; 26];x = ones(1,length(b));[m,n] = size(A);c = zeros(m,n);d = zeros(m,1);ea = 100;tol = 1;iter = 1;disp('Iter x1 x2 x3 Max_Error')fprintf('%d %9.4f %9.4f %9.4f %9.4f\n', iter-1, x(1),x(2),x(3),ea)while ea > tol for i = 1:n d(i) = b(i)/A(i,i); for j = 1:m if i == j c(i,j) = 0; else c(i,j) = A(i,j)/A(i,i); end5end x_new(i) = d(i) - c(i,:)*x(:); ea_vect(i) = abs((x_new(i) - x(i))/x_new(i)*100); x(i) = x_new(i); end ea = max(ea_vect); fprintf('%d %9.4f %9.4f %9.4f %9.4f\n', iter, x(1),x(2),x(3),ea) iter = iter + 1;endIter x1 x2 x3 Max_Error0 1.0000 1.0000 1.0000 100.00001 8.8000 -1.9333 4.6000 151.72412 9.7467 -2.8489 4.9600 32.13733 9.9658 -2.9819 4.9960 4.46144 9.9960 -2.9980 4.9996 0.5360Problem 2. Gauss EliminationclearclcA = [10 2 -1; 2 6 1; 1 3 5];b = [89; 7; 26];D = [A b]n = size(A,1);aux = D(1,:);D(1,:) = D(2,:);D(2,:) = aux;for i = 1:n D(i,:) = D(i,:)/D(i,i); for j = i+1:n D(j,:) = (-D(j,i))*D(i,:) + D(j,:); endendDfor i = n:-1:1 for j = i-1:-1:1 D(j,:) = (-D(j,i))*D(i,:) + D(j,:); endendDx = D(:,end)D = 10 2 -1 89 2


View Full Document

UT Dallas MATH 4334 - Lab Assignment 2

Documents in this Course
Load more
Download Lab Assignment 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lab Assignment 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lab Assignment 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?