MATLAB Examples

# Example of Constrained Polynomials

(c) 2013 Matthew Harker and Paul O'Leary

This program demonstrated the use of the function polyconstrained.m to generate constrained polynomials. The constraints on the polynomials may be arbitrary. They are defined in terms of values of derivatives at specific points: e.g.:

That is the derivative of at the position has the value .

## Contents

```close all; clear all; % FontSize = 12; set(0,'DefaultaxesFontName','Times'); set(0,'DefaultaxesFontSize',FontSize); set(0,'DefaulttextFontName','Times'); set(0,'DefaulttextFontSize',FontSize); set(0,'DefaultfigurePaperType','A4'); set(0,'DefaultTextInterpreter', 'latex'); % ```

## Example: A Zero Constraint at a Point not Corresponding to a Node

The vector is a column vector containing the positions at which the polynomials are to be evaluated.

```nrPts = 11; xmin = 0; xmax = 1; % x = linspace(xmin,xmax,nrPts)'; % ```

define a zero constraint i.e. y(0.25) = 0, x = 0.25 does not correspond to a node More formally this constraint is defined as

```D^(0) y(0.25) = 0
```

Consequently the floowing vector defined this constraint.

```C = [0, 0.25, 0]; % % Generate the polynomials % d= 5; [Vh, yp] = polyconstrained( x, d, C ); ```

Plot the particular solution and the first 4 homogeneously constrained polynomials.

```fig0 = figure; plot( x, yp, 'r'); hold on; % nrBfs = 4; for k=1:nrBfs plot( x, Vh(:,end-k+1), 'k'); hold on; end; grid on; % % Plot the nodes on the x axis % plot( x, zeros(size(x)), 'ko', 'markerfaceColor', 'w'); % % Plot the position of the zero constraint % plot( C(1,2), 0, 'ko', 'markerfaceColor', 'k'); % legend( 'Particular', 'Homogeneous','Location','SouthWest'); xlabel('Support \$\$x\$\$'); ylabel('\$\$y(x)\$\$'); title('Zero Constraint not at a Node'); % ```

## Example: A Constraint Outside the Range of the Support

The vector is a column vector containing the positions at which the polynomials are to be evaluated.

```nrPts = 100; xmin = 0; xmax = 1; % x = linspace(xmin,xmax,nrPts)'; % ```

define a the constraint at 1.1 , i.e. outside the range

```C = [0, 1.1, 1]; % d= 5; [Vh, yp, pp, L] = polyconstrained( x, d, C ); ```

Extrapolate to the constrained point

```xe = linspace(xmin,xmax+0.15,nrPts)'; Ve = polyvander(xe, d ); Ye = Ve * L; % ype = Ve * pp; ```
```fig0 = figure; plot( x, yp, 'r'); hold on; % % Plot the extrapolated homogeneous polynomials % plot( xe, Ye, 'k:'); plot( xe, ype, 'r:'); % nrBfs = 5; for k=1:nrBfs plot( x, Vh(:,end-k+1), 'k','LineWidth',2); hold on; end; grid on; % % Plot the position of the zero constraint % plot( C(1,2), 0, 'ko', 'markerfaceColor', 'k'); plot( C(1,2), C(1,3), 'ro', 'markerfaceColor', 'r'); % legend( 'Particular', 'Homogeneous','Location','North'); xlabel('Support \$\$x\$\$'); ylabel('\$\$y(x)\$\$'); title('Zero Constraint Outside the Range'); % ```

## Example: Polynomial with Value and Derivative Constraints

The vector is a column vector containing the positions at which the polynomials are to be evaluated.

```nrPts = 100; xmin = 0; xmax = 1; % x = linspace(xmin,xmax,nrPts)'; ```

The following constraints are typical for an initial value problem. Define the constraints: and . There are two constraints,

and

\$.

These constraints are defined as follows:

```c1 = [0, x(1), 1]; c2 = [1, x(1), -1]; C = [c1; c2]; % % Define the degree of the polynomials % d =10; % % Generate the polynomials and particular functions. % [Vh, yp, pp, L, S] = polyconstrained( x, d, C ); ```

Plot the particular solution yp

```fig1 = figure; plot( x, yp, 'r'); hold on; % % Plot the homogeneously constrained polynomials Vh in order of increasing % degree % nrBfs = 5; for k=1:nrBfs plot( x, Vh(:,end-k+1), 'k'); hold on; end; grid on; % legend( 'Particular', 'Homogeneous','Location','SouthWest'); xlabel('Support \$\$x\$\$'); ylabel('\$\$y(x)\$\$'); title('Initial Value Constraints'); % % Use the S.eqns cells to document the constraints. % text(0.1, 0.5,S.eqns(1),'BackgroundColor','w'); text(0.1, 0.3,S.eqns(2),'BackgroundColor','w'); % ```

## Boundary Value Constraints: Example 1

The vector is a column vector containing the positions at which the polynomials are to be evaluated.

```nrPts = 100; xmin = -1; xmax = 1; % x = linspace(xmin,xmax,nrPts)'; % ```

define the constraints corresponding to a doubly held beam

`\$\$ y(0) = 0, y(1) = 0, D y(0) = 0, D y(1) = 0 \$\$`
```c1 = [0, x(1), 0]; c2 = [0, x(end), 0]; c3 = [1, x(1), 0]; c4 = [1, x(end), 0]; % C = [c1; c2; c3; c4]; % ```

Define the degree of the polynomials

```d =10; % % Generate the polynomials and particular functions. % [Vh, yp, pp, L, S] = polyconstrained( x, d, C ); % ```

Plot the particular solution yp

```fig2 = figure; plot( x, yp, 'r'); hold on; % % Plot the homogeneously constrained polynomials Vh in order of increasing % degree % nrBfs = 5; for k=1:nrBfs plot( x, Vh(:,end-k+1), 'k'); hold on; end; grid on; % legend( 'Particular', 'Homogeneous','Location','SouthWest'); xlabel('Support \$\$x\$\$'); ylabel('\$\$y(x)\$\$'); title('Boundary Value Constraints'); % % Use the S.eqns cells to document the constraints. % text(-0.95, 0.45,S.eqns(1),'BackgroundColor','w'); text(-0.45, 0.45,S.eqns(2),'BackgroundColor','w'); text(0.05, 0.45,S.eqns(3),'BackgroundColor','w'); text(0.55, 0.45,S.eqns(4),'BackgroundColor','w'); % ```

## Boundary Value Constraints: Example 2

The vector is a column vector containing the positions at which the polynomials are to be evaluated.

```nrPts = 100; xmin = 0; xmax = 1; % x = linspace(xmin,xmax,nrPts)'; % ```

define the constraints corresponding to a cantilever

```\$\$ y(0) = 0\$\$
\$\$ D y(0) = 0\$\$
\$\$ D^2 y(1) = 0\$\$
\$\$ D^3 y(1) = 0\$\$```
```c1 = [0, x(1), 0]; c2 = [1, x(1), 0]; c3 = [2, x(end), 0]; c4 = [3, x(end), 0]; % C = [c1; c2; c3; c4]; % ```

Define the degree of the polynomials

```d =10; % % Generate the polynomials and particular functions. % [Vh, yp, pp, L, S] = polyconstrained( x, d, C ); % ```

Plot the particular solution yp

```fig2 = figure; plot( x, yp, 'r'); hold on; % % Plot the homogeneously constrained polynomials Vh in order of increasing % degree % nrBfs = 5; for k=1:nrBfs plot( x, Vh(:,end-k+1), 'k'); hold on; end; grid on; % legend( 'Particular', 'Homogeneous','Location','NorthEast'); xlabel('Support \$\$x\$\$'); ylabel('\$\$y(x)\$\$'); title('Boundary Value Constraints'); % % Use the S.eqns cells to document the constraints. % text(0.01, -0.45,S.eqns(1),'BackgroundColor','w'); text(0.25, -0.45,S.eqns(2),'BackgroundColor','w'); text(0.5, -0.45,S.eqns(3),'BackgroundColor','w'); text(0.75, -0.45,S.eqns(4),'BackgroundColor','w'); ```