Added Helps for Homework 3.2

This commit is contained in:
2018-11-02 01:35:23 -04:00
parent 2a245cbef2
commit 83e17b81a6
2 changed files with 142 additions and 0 deletions

135
Homework/Homework32.m Normal file
View File

@@ -0,0 +1,135 @@
%This is a script to do the homework for 3.2
%3.1a
disp('Problem 3.1.a:')
disp('')
xValues = [8.1 8.3 8.6 8.7];
x = 8.4;
P0Values = [16.94410 17.56492 18.50515 18.82091];
P1Values = [];
P2Values = [];
for n = 2:size(P0Values)(2)
P1Values(end + 1) = Nevilles(P0Values(n - 1), P0Values(n), xValues(n - 1), xValues(n), x);
end
for n = 2:size(P1Values)(2)
P2Values(end + 1) = Nevilles(P1Values(n - 1), P1Values(n), xValues(n - 1), xValues(n + 1), x);
end
P3Values = Nevilles(P2Values(1), P2Values(2), xValues(1), xValues(4), x);
for n = 1:size(P0Values)(2)
disp(['P' num2str(n - 1, '%8.8g') ' = ' num2str(P0Values(n), '%8.8g')])
end
for n = 1:size(P1Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n, '%8.8g') ' = ' num2str(P1Values(n), '%8.8g')])
end
for n = 1:size(P2Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 1, '%8.8g') ' = ' num2str(P2Values(n), '%8.8g')])
end
disp(['P03 = ' num2str(P3Values, '%8.8g')])
%3.3
disp('')
disp('')
disp('Problem 3.3.a')
x = 1/2;
P0Values = [];
P1Values = [];
P2Values = [];
P3Values = [];
P4Values = [];
xValues = [-2 -1 0 1 2];
f = @(x) 3^x;
for n = 1:size(xValues)(2)
P0Values(end + 1) = f(xValues(n));
end
for n = 2:size(P0Values)(2)
P1Values(end + 1) = Nevilles(P0Values(n - 1), P0Values(n), xValues(n - 1), xValues(n), x);
end
for n = 2:size(P1Values)(2)
P2Values(end + 1) = Nevilles(P1Values(n - 1), P1Values(n), xValues(n - 1), xValues(n + 1), x);
end
for n = 2:size(P2Values)(2)
P3Values(end + 1) = Nevilles(P2Values(n - 1), P2Values(n), xValues(n - 1), xValues(n + 2), x);
end
for n = 2:size(P3Values)(2)
P4Values(end + 1) = Nevilles(P3Values(n - 1), P3Values(n), xValues(n - 1), xValues(n + 3), x);
end
%Print the results
for n = 1:size(P0Values)(2)
disp(['P' num2str(n - 1, '%8.8g') ' = ' num2str(P0Values(n), '%8.8g')])
end
for n = 1:size(P1Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n, '%8.8g') ' = ' num2str(P1Values(n), '%8.8g')])
end
for n = 1:size(P2Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 1, '%8.8g') ' = ' num2str(P2Values(n), '%8.8g')])
end
for n = 1:size(P3Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 2, '%8.8g') ' = ' num2str(P3Values(n), '%8.8g')])
end
for n = 1:size(P4Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 3, '%8.8g') ' = ' num2str(P4Values(n), '%8.8g')])
end
aAnswer = P4Values;
disp('')
disp('')
disp('Problem 3.3.b')
x = 3;
P0Values = [];
P1Values = [];
P2Values = [];
P3Values = [];
P4Values = [];
xValues = [0 1 2 4 5];
f = @(x) sqrt(x);
for n = 1:size(xValues)(2)
P0Values(end + 1) = f(xValues(n));
end
for n = 2:size(P0Values)(2)
P1Values(end + 1) = Nevilles(P0Values(n - 1), P0Values(n), xValues(n - 1), xValues(n), x);
end
for n = 2:size(P1Values)(2)
P2Values(end + 1) = Nevilles(P1Values(n - 1), P1Values(n), xValues(n - 1), xValues(n + 1), x);
end
for n = 2:size(P2Values)(2)
P3Values(end + 1) = Nevilles(P2Values(n - 1), P2Values(n), xValues(n - 1), xValues(n + 2), x);
end
for n = 2:size(P3Values)(2)
P4Values(end + 1) = Nevilles(P3Values(n - 1), P3Values(n), xValues(n - 1), xValues(n + 3), x);
end
for n = 1:size(P0Values)(2)
disp(['P' num2str(n - 1, '%8.8g') ' = ' num2str(P0Values(n), '%8.8g')])
end
for n = 1:size(P1Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n, '%8.8g') ' = ' num2str(P1Values(n), '%8.8g')])
end
for n = 1:size(P2Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 1, '%8.8g') ' = ' num2str(P2Values(n), '%8.8g')])
end
for n = 1:size(P3Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 2, '%8.8g') ' = ' num2str(P3Values(n), '%8.8g')])
end
for n = 1:size(P4Values)(2)
disp(['P' num2str(n - 1, '%8.8g') num2str(n + 3, '%8.8g') ' = ' num2str(P4Values(n), '%8.8g')])
end
bAnswer = P4Values;
disp('')
disp('')
disp('Problem 3.3.c')
errorA = abs(sqrt(3) - aAnswer);
errorB = abs(sqrt(3) - bAnswer);
disp(['The absolute error in part a is ' num2str(errorA, '%8.8g')])
disp(['The absolute error in part b is ' num2str(errorB, '%8.8g')])
clear xValues x P0Values P1Values P2Values P3Values P4Values n aAnswer bAnswer errorA errorB f

7
Nevilles.m Normal file
View File

@@ -0,0 +1,7 @@
function [value] = Nevilles(P0, P1, x0, x1, x)
%
%Nevilles(P0, P1, x0, x1, x)
%This is function to calculate Pn using Nevilles method
%
value = (((x - x1)/(x0 - x1)) * P0) + (((x - x0)/(x1 - x0)) * P1);
end