Matlab #2 PDF

Title Matlab #2
Author Janiquea Gray
Course Calculus I
Institution New Jersey Institute of Technology
Pages 3
File Size 50.9 KB
File Type PDF
Total Downloads 92
Total Views 141

Summary

Matlab assignment ...


Description

Janiquea Gray Math 111 Professor Ward December 1, 2017 MATLAB Assignment 2

1. Command: x = 1; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=-1; % because f(1)=-1 fprintf('step x dx f(x)\n') fprintf('---- ----------- --------- ----------\n') fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = 3*x^2 - 2; xnew = x - (f/fprime); % compute the new value of x dx=abs(x-xnew); % compute how much x has changed since last step x = xnew; f = x^3 - 2*x; % compute the new value of f(x) fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) end >> Output step x dx f(x) ---- ----------- --------- ---------0 1.00000000 1.00000000 -1.00000000 1 2.00000000 1.00000000 4.00000000 2 1.60000000 0.40000000 0.89600000 3 1.44225352 0.15774648 0.11551761 4 1.41501064 0.02724288 0.00319099 5 1.41421424 0.00079640 0.00000269 6 1.41421356 0.00000067 0.00000000 7 1.41421356 0.00000000 0.00000000 Command: x = -1; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=1; % because f(-1)=1

fprintf('step x dx f(x)\n') fprintf('---- ----------- --------- ----------\n') fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = 3*x^2 - 2; xnew = x - (f/fprime); % compute the new value of x dx=abs(x-xnew); % compute how much x has changed since last step x = xnew; f = x^3 - 2*x; % compute the new value of f(x) fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) end >> MatlabCalc111 step x dx f(x) ---- ----------- --------- ---------0 -1.00000000 1.00000000 1.00000000 1 -2.00000000 1.00000000 -4.00000000 2 -1.60000000 0.40000000 -0.89600000 3 -1.44225352 0.15774648 -0.11551761 4 -1.41501064 0.02724288 -0.00319099 5 -1.41421424 0.00079640 -0.00000269 6 -1.41421356 0.00000067 -0.00000000 7 -1.41421356 0.00000000 -0.00000000 x = 0; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=0; % because f(0)=0 fprintf('step x dx f(x)\n') fprintf('---- ----------- --------- ----------\n') fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = 3*x^2 - 2; xnew = x - (f/fprime); % compute the new value of x dx=abs(x-xnew); % compute how much x has changed since last step x = xnew; f = x^3 - 2*x; % compute the new value of f(x) fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) end

>> Output step x dx f(x) ---- ----------- --------- ---------0 0.00000000 1.00000000 0.00000000 1 0.00000000 0.00000000 0.00000000 What happens if you give an initial guess of x=0? If the initial guess is zero, the value f (0) does not pass the conditions of the while loop and therefore short-circuits the program causing it to only calculate variable knew once after it initializes. Although dx is greater than the tolerance level, f (0) is not. 2. Command: x = 0; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=1; % because f(0)=1 fprintf('step x dx f(x)\n') fprintf('---- ----------- --------- ----------\n') fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = -sin(x) -3; xnew = x - (f/fprime); % compute the new value of x dx=abs(x-xnew); % compute how much x has changed since last step x = xnew; % compute the new value of f(x) f = cos(x)-3*x; fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f) end >> Output step x dx f(x) ---- ----------- --------- ---------0 0.00000000 1.00000000 1.00000000 1 0.33333333 0.33333333 -0.05504305 2 0.31678995 0.01654338 -0.00012955 3 0.31675083 0.00003912 -0.00000000 4 0.31675083 0.00000000 0.00000000 How many iterations are required to obtain accuracy of 6 decimal places? Between one and four iterations are required to obtain accuracy of 6 decimal places....


Similar Free PDFs