


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Polynomials Practice Problems Engineering Computation, Dr Simin Nasseri
5
3
5 3
p=[1 0 0 0 5 7]
h=[0 0 1 0 0 2]
p+h = [1 0 1 0 5 9]
mult=conv(p,h)
mu
p x x x
h x x
P x h x x x x
P x h x
8 5 4 3
2
lt= 2 5 7 10 14
Works if you omit the first two zeros of h.
p=[1 0 0 0 5 7]
h=[1 0 0 2]
[quo, rem]=deconv(p,h)
x x x x x
P x h x x
2
2
Quotient
Remainder = 2 5 7
polyval(p,-1)
polyval(h,2)
x
x x
h
5
p x ( ) x 5 x 7
% polynomial = array of coefficients in matlab:
p = [1 0 0 0 5 7]; % p(x) = x^5 + 5*x + 7
format long; % print double-precision
roots(p) % print out the roots of p(x)
2- Consider the following sets of x and y vectors. Plot the graphs and find the best polynomial
which can fit each set (write the equation in front of it). Write down the type of fit (degree of
polynomials and its coefficients), resides and normresids values.
x
y
Cubic fit:
3
y x 1 (p 1 = 1, p 2 = p 3 = 0, p 4 = -1)
x
y
Forth degree polynomial:
function [a , b] = PhillipChris
% This function reads two input files and fits a curve to each data set
% It also interpolates the values given for much finer data values.
% Written by S. Nasseri
clc
close all
clear all
% Loading the data and allocating them as x and y vectors:
load Phillip.txt
load Chris.txt
x1 = Phillip(:,1);
y1 = Phillip(:,2);
x2 = Chris(:,1);
y2 = Chris(:,2);
% generating much finer data points based on the original x values:
x1new = x1(1): 0.1: x1(end);
x2new = x2(1): 0.1: x2(end);
%___________________________________________________________________
% Interpolation, generating new y values based on initial finer data points
y1new = interp1(x1, y1, x1new, 'spline');
y2new = interp1(x2, y2, x2new, 'spline');
figure (1)
plot(x1,y1,'r*', x1new,y1new)
hold on
plot(x2,y2,'g*', x2new,y2new, 'k')
text (.5,200,'Interpolation')
%___________________________________________________________________
% Curve Fitting:
% finding the coefficients of the two cubic polynomials:
% We should use the original set of data:
format long
a = polyfit(x1, y1, 3);
b = polyfit(x2, y2, 3);
% Generating the corresponding y values based on the polynomials we found:
% Here we use the finer data:
y_fit1 = polyval(a, x1new);
y_fit2 = polyval(b, x2new);
figure (2)
plot(x1,y1,'r*', x1new,y_fit1)
fprintf('Phillip''s data were fitted with y= %3.2f x^3+ %3.2f x^2+%3.2f x+
%3.2f\n',a);
hold on
plot(x2,y2,'g*', x2new,y_fit2, 'k')
fprintf('Chris''s data were fitted with y= %3.2f x^3+ %3.2f x^2+%3.2f x+ %3.2f\
n',b);
text (.5,200,'Curve Fitting')