From 4d1b4187ef2e01b813287f43701a48e8b525e07e Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 6 Nov 2018 19:27:36 -0500 Subject: [PATCH] Added functions for homework 3.6 --- Bezier.m | 38 ++++++++++++++++ Hermite.m | 38 ++++++++++++++++ Homework/Homework36.m | 100 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 Bezier.m create mode 100644 Hermite.m create mode 100644 Homework/Homework36.m diff --git a/Bezier.m b/Bezier.m new file mode 100644 index 0000000..6658f03 --- /dev/null +++ b/Bezier.m @@ -0,0 +1,38 @@ +function [x, y, xNums, yNums] = Bezier(point0, point1, control0, control1) +% +%This function returns a function handle to the hermite polynomial for the points given +%[x, y, xNums, yNums] = Hermite(point0, point1, control0, control1) +%All of the parameters are [x, y] coordinates +%x is a function handle for the polynomial for x. x(t) +%y is a function handle for the polynomial for y. y(t) +%xNums is an array with the numbers for the x polynomial, starting a t^3 and working down +%yNums is an array with the numbers for the y polynomial, starting a t^3 and working down +% + + xNums = [0, 0, 0, 0]; + yNums = [0, 0, 0, 0]; + a = [(control0(1) - point0(1)), (point1(1) - control1(1))]; + b = [(control0(2) - point0(2)), (point1(2) - control1(2))]; + + %t^3 number + xNums(1) = (2 * (point0(1) - point1(1))) + (3 * (a(1) + a(2))); + %t^2 number + xNums(2) = (3 * (point1(1) - point0(1))) - (3 * (a(2) + (2 * a(1)))); + %t number + xNums(3) = (3 * a(1)); + %constant + xNums(4) = point0(1); + %Function handle + x = @(t) (xNums(1) * t.^3) + (xNums(2) * t.^2) + (xNums(3) * t) + xNums(4); + + %The t^3 number + yNums(1) = (2 * (point0(2) - point1(2))) + (3 * (b(1) + b(2))); + %The t^2 number + yNums(2) = (3 * (point1(2) - point0(2))) - (3 * (b(2) + (2 * b(1)))); + %The t number + yNums(3) = (3 * b(1)); + %The constant + yNums(4) = point0(2); + %Function handle + y = @(t) (yNums(1) * t.^3) + (yNums(2) * t.^2) + (yNums(3) * t) + yNums(4); +end diff --git a/Hermite.m b/Hermite.m new file mode 100644 index 0000000..5e3e97b --- /dev/null +++ b/Hermite.m @@ -0,0 +1,38 @@ +function [x, y, xNums, yNums] = Hermite(point0, point1, control0, control1) +% +%This function returns a function handle to the hermite polynomial for the points given +%[x, y, xNums, yNums] = Hermite(point0, point1, control0, control1) +%All of the parameters are [x, y] coordinates +%x is a function handle for the polynomial for x. x(t) +%y is a function handle for the polynomial for y. y(t) +%xNums is an array with the numbers for the x polynomial, starting a t^3 and working down +%yNums is an array with the numbers for the y polynomial, starting a t^3 and working down +% + + xNums = [0, 0, 0, 0]; + yNums = [0, 0, 0, 0]; + a = [(control0(1) - point0(1)), (point1(1) - control1(1))]; + b = [(control0(2) - point0(2)), (point1(2) - control1(2))]; + + %t^3 number + xNums(1) = (2 * (point0(1) - point1(1))) + (a(1) + a(2)); + %t^2 number + xNums(2) = (3 * (point1(1) - point0(1))) - (a(2) + (2 * a(1))); + %t number + xNums(3) = (a(1)); + %constant + xNums(4) = point0(1); + %Function handle + x = @(t) (xNums(1) * t.^3) + (xNums(2) * t.^2) + (xNums(3) * t) + xNums(4); + + %The t^3 number + yNums(1) = (2 * (point0(2) - point1(2))) + (b(1) + b(2)); + %The t^2 number + yNums(2) = (3 * (point1(2) - point0(2))) - (b(2) + (2 * b(1))); + %The t number + yNums(3) = (b(1)); + %The constant + yNums(4) = point0(2); + %Function handle + y = @(t) (yNums(1) * t.^3) + (yNums(2) * t.^2) + (yNums(3) * t) + yNums(4); +end diff --git a/Homework/Homework36.m b/Homework/Homework36.m new file mode 100644 index 0000000..282d875 --- /dev/null +++ b/Homework/Homework36.m @@ -0,0 +1,100 @@ +%This is a script for the homework for 3.6 + +%3.6.1.a +point0 = [0, 0]; +point1 = [5, 2]; +control0 = [1, 1]; +control1 = [6, 1]; +t = 0:0.01:1; +[x, y, xNums, yNums] = Hermite(point0, point1, control0, control1); +%Display the polynomial itself +disp('Problem 3.1.a') +disp(['x(t) = ' num2str(xNums(1)) 't^3 + ' num2str(xNums(2)) 't^2 + ' num2str(xNums(3)) 't + ' num2str(xNums(4))]) +disp(['y(t) = ' num2str(yNums(1)) 't^3 + ' num2str(yNums(2)) 't^2 + ' num2str(yNums(3)) 't + ' num2str(yNums(4))]) +figure +hold on; +%Plot the points +plot(control0(1), control0(2), 'o') +plot(control1(1), control1(2), 'o') +plot(point0(1), point0(2), 'o') +plot(point1(1), point1(2), 'o') +%Plot the line from the first control point to the first point as a dotted line +plot([control0(1), point0(1)], [control0(2), point0(2)], ':') +%Plot the line from the last control point to the last point as a dotted line +plot([control1(1), point1(1)], [control1(2), point1(2)], ':') +%Plot the curve between the two points +plot(x(t), y(t)) + + +%3.6.1.b +control0 = [0.5, 0.5]; +control1 = [5.5, 1.5]; +[x, y, xNums, yNums] = Hermite(point0, point1, control0, control1); +%Display the polynomial itself +disp('') +disp('Problem 3.1.b') +disp(['x(t) = ' num2str(xNums(1)) 't^3 + ' num2str(xNums(2)) 't^2 + ' num2str(xNums(3)) 't + ' num2str(xNums(4))]) +disp(['y(t) = ' num2str(yNums(1)) 't^3 + ' num2str(yNums(2)) 't^2 + ' num2str(yNums(3)) 't + ' num2str(yNums(4))]) +figure +hold on; +%Plot the points +plot(control0(1), control0(2), 'o') +plot(control1(1), control1(2), 'o') +plot(point0(1), point0(2), 'o') +plot(point1(1), point1(2), 'o') +%Plot the line from the first control point to the first point as a dotted line +plot([control0(1), point0(1)], [control0(2), point0(2)], ':') +%Plot the line from the last control point to the last point as a dotted line +plot([control1(1), point1(1)], [control1(2), point1(2)], ':') +%Plot the curve between the two points +plot(x(t), y(t)) + + +%3.6.2.a +control0 = [1, 1]; +control1 = [6, 1]; +t = 0:0.01:1; +[x, y, xNums, yNums] = Bezier(point0, point1, control0, control1); +%Display the polynomial itself +disp('') +disp('Problem 3.6.2.a') +disp(['x(t) = ' num2str(xNums(1)) 't^3 + ' num2str(xNums(2)) 't^2 + ' num2str(xNums(3)) 't + ' num2str(xNums(4))]) +disp(['y(t) = ' num2str(yNums(1)) 't^3 + ' num2str(yNums(2)) 't^2 + ' num2str(yNums(3)) 't + ' num2str(yNums(4))]) +figure +hold on; +%Plot the points +plot(control0(1), control0(2), 'o') +plot(control1(1), control1(2), 'o') +plot(point0(1), point0(2), 'o') +plot(point1(1), point1(2), 'o') +%Plot the line from the first control point to the first point as a dotted line +plot([control0(1), point0(1)], [control0(2), point0(2)], ':') +%Plot the line from the last control point to the last point as a dotted line +plot([control1(1), point1(1)], [control1(2), point1(2)], ':') +%Plot the curve between the two points +plot(x(t), y(t)) + +%3.6.2.b +control0 = [0.5, 0.5]; +control1 = [5.5, 1.5]; +[x, y, xNums, yNums] = Bezier(point0, point1, control0, control1); +%Display the polynomial itself +disp('') +disp('Problem 3.6.2.b') +disp(['x(t) = ' num2str(xNums(1)) 't^3 + ' num2str(xNums(2)) 't^2 + ' num2str(xNums(3)) 't + ' num2str(xNums(4))]) +disp(['y(t) = ' num2str(yNums(1)) 't^3 + ' num2str(yNums(2)) 't^2 + ' num2str(yNums(3)) 't + ' num2str(yNums(4))]) +figure +hold on; +%Plot the points +plot(control0(1), control0(2), 'o') +plot(control1(1), control1(2), 'o') +plot(point0(1), point0(2), 'o') +plot(point1(1), point1(2), 'o') +%Plot the line from the first control point to the first point as a dotted line +plot([control0(1), point0(1)], [control0(2), point0(2)], ':') +%Plot the line from the last control point to the last point as a dotted line +plot([control1(1), point1(1)], [control1(2), point1(2)], ':') +%Plot the curve between the two points +plot(x(t), y(t)) + +clear control0 control1 point0 point1 t x xNums y yNums \ No newline at end of file