MATLAB Conversion of Polymath Problems

Drivers for the NLE and ODE functions

1. Nonlinear Equations - One Implicit Equation

In this case the driver program is of the form:  OneNLEtemplate.m  (right click to save this file)

function % Insert here your file name after function (Use Alphanumberic name only)
clear, clc, format short g, format compact
xguess= % Replace this line with the xguess line(s) from Polymath report.
disp('Variable values at the initial estimate');
disp([' Unknown value ' num2str(xguess) ' Function Value ' num2str(NLEfun(xguess))]);
xsolv=fzero(@NLEfun,xguess);
disp(' Variable values at the solution');
disp([' Unknown value ' num2str(xsolv) ' Function Value ' num2str(NLEfun(xsolv))]);
%- - - - - - - - - - - - - - - - - - - - - -
% Replace this and the following line with the function copied from the Polymath report
% Do not include the xguess line(s)

 

Example from the Polymath HELP section

  1. Polymath File   OneNLEexample.pol  (right click to save this file)

f(T) = x1 * P1 + x2 * P2 - 760

P1 = 10 ^ (6.85221 - 1064.63 / (T + 232.0))

P2 = 10 ^ (6.87776 - 1171.53 / (T + 224.366))

x1 = 0.1

x2 = 0.9

T(min) = 36

T(max) = 70

 

  1. MATLAB file   OneNLEexample.m  (right click to save this file)

function OneNLEexample

clear, clc, format short g, format compact

xguess = 53. ;

disp('Variable values at the initial estimate');

disp([' Unknown value ' num2str(xguess) ' Function Value ' num2str(NLEfun(xguess))]);

xsolv=fzero(@NLEfun,xguess);

disp(' Variable values at the solution');

disp([' Unknown value ' num2str(xsolv) ' Function Value ' num2str(NLEfun(xsolv))]);

%- - - - - - - - - - - - - - - - - - - - - -

function fT = NLEfun(T);

P1 = 10 ^ (6.85221 - 1064.63 / (T + 232.0));

P2 = 10 ^ (6.87776 - 1171.53 / (T + 224.366));

x1 = 0.1;

x2 = 0.9;

fT = x1 * P1 + x2 * P2 - 760;

  1. MATLAB results

>> Variable values at the initial estimate

 Unknown value 53   Function Value -223.4561

 Variable values at the solution

 Unknown value 63.6645   Function Value 1.1369e-013

 

2. Nonlinear Equations - Several Implicit Equations

In this case the driver program is of the form:   MultipleNLEtemplate.m  (right click to save this file)

function % Insert here your file name after function (Use Alphanumberic names only)

clear, clc, format short g, format compact

xguess=     % Replace this line with the xguess line(s) from Polymath report.

disp('Variable values at the initial estimate');

fguess=MNLEfun(xguess);

disp(' Variable        Value     Function Value')

for i=1:size(xguess,2);

disp([' x' int2str(i) '                 ' num2str(xguess(i)) '            ' num2str(fguess(i))]);

end

options = optimset('Diagnostics',['off'],'TolFun',[1e-9],'TolX',[1e-9]);

xsolv=fsolve(@MNLEfun,xguess,options);

disp('Variable values at the solution');

fsolv=MNLEfun(xsolv);

disp(' Variable        Value     Function Value')

for i=1:size(xguess,2);

disp([' x' int2str(i) '                    ' num2str(xsolv(i)) '            ' num2str(fsolv(i))])

end

%- - - - - - - - - - - - - - - - - - - - - -

% Replace this and the following line with the function copied from the Polymath report

% Do not include the xguess line(s)

 

Example from the Polymath HELP section

  1. Polymath File   MultipleNLEexample.pol  (right click to save this file)

f(CA1) = k * CA1 ^ 2 - v * (CA0 - CA1) / V

f(V) = k * CA2 ^ 2 - v * (CA1 - CA2) / V

k = 0.075

v = 30

CA0 = 1.6

CA2 = 0.2 * CA0

CA1(0) = 1

V(0) = 300

 

  1. MATLAB file   MultipleNLEexample.m  (right click to save this file)

function MultipleNLEexample

clear, clc, format short g, format compact

xguess = [1. 300.]; % initial guess vector

disp('Variable values at the initial estimate');

fguess=MNLEfun(xguess);

disp(' Variable        Value     Function Value')

for i=1:size(xguess,2);

disp([' x' int2str(i) '                 ' num2str(xguess(i)) '            ' num2str(fguess(i))]);

end

options = optimset('Diagnostics',['off'],'TolFun',[1e-9],'TolX',[1e-9]);

xsolv=fsolve(@MNLEfun,xguess,options);

disp('Variable values at the solution');

fsolv=MNLEfun(xsolv);

disp(' Variable        Value     Function Value')

for i=1:size(xguess,2);

disp([' x' int2str(i) '                    ' num2str(xsolv(i)) '            ' num2str(fsolv(i))])

end

%- - - - - - - - - - - - - - - - - - - - - -

function fx = MNLEfun(x);

CA1 = x(1);

V = x(2);

k = 0.075;

v = 30;

CA0 = 1.6;

CA2 = 0.2 * CA0;

fx(1,1) = k * CA1 ^ 2 - v * (CA0 - CA1) / V;

fx(2,1) = k * CA2 ^ 2 - v * (CA1 - CA2) / V;

 

  1. MATLAB results

>> Variable values at the initial estimate

 Variable        Value     Function Value

 x1                 1            0.015

 x2                 300            -0.06032

Optimization terminated successfully:

 Relative function value changing by less than OPTIONS.TolFun

Variable values at the solution

 Variable        Value     Function Value

 x1                    0.602            -3.8154e-012

 x2                    1101.5493            -1.2084e-012

 

3.  Differential equations

In this case the driver program is of the form:   MultipleDEQtemplate.m  (right click to save this file)

function % Insert here your file name after function (Use Alphanumberic names only)

clear, clc, format short g, format compact

tspan=  % Replace this line with tspan line from Polymath report

y0=     % Replace this line with y0 line from Polymath report

disp(' Variable values at the initial point ');

disp([' t    = ' num2str(tspan(1))]);

disp('           y                  dy/dt         ');

disp([y0 ODEfun(tspan(1),y0)]);

[t,y]=ode45(@ODEfun,tspan,y0);

for i=1:size(y,2)

    disp([' Solution for dependent variable y' int2str(i)]);

    disp(['              t                  y' int2str(i)]);

    disp([t y(:,i)]);

    plot(t,y(:,i));

    title([' Plot of dependent variable y' int2str(i)]);

    xlabel(' Independent variable (t)');

    ylabel([' Dependent variable y' int2str(i)]);

    pause

end

%- - - - - - - - - - - - - - - - - - - - - -

% Replace this and the following line with the function copied from the Polymath report

% Do not include the tspan and y0 lines

 

Example from the Polymath HELP section   MultipleDEQexample.pol  (right click to save this file)

  1. Polymath File

d(A)/d(t) = -k1 * A

d(B)/d(t) = k1 * A - k2 * B

d(C)/d(t) = k2 * B

k1 = 1

k2 = 2

A(0) = 1

B(0) = 0

C(0) = 0

t(0) = 0

t(f) = 3

 

  1. MATLAB file   MultipleDEQexample.m  (right click to save this file)

function MultipleDEQexample

clear, clc, format short g, format compact

tspan = [0 3.]; % Range for the independent variable

y0 = [1.; 0; 0]; % Initial values for the dependent variables

disp(' Variable values at the initial point ');

disp([' t    = ' num2str(tspan(1))]);

disp('           y                  dy/dt         ');

disp([y0 ODEfun(tspan(1),y0)]);

[t,y]=ode45(@ODEfun,tspan,y0);

for i=1:size(y,2)

    disp([' Solution for dependent variable y' int2str(i)]);

    disp(['              t                  y' int2str(i)]);

    disp([t y(:,i)]);

    plot(t,y(:,i));

    title([' Plot of dependent variable y' int2str(i)]);

    xlabel(' Independent variable (t)');

    ylabel([' Dependent variable y' int2str(i)]);

    pause

end

%- - - - - - - - - - - - - - - - - - - - - -

function dYfuncvecdt = ODEfun(t,Yfuncvec);

A = Yfuncvec(1);

B = Yfuncvec(2);

C = Yfuncvec(3);

k1 = 1;

k2 = 2;

dAdt = -k1 * A;

dBdt = k1 * A - k2 * B;

dCdt = k2 * B;

dYfuncvecdt = [dAdt; dBdt; dCdt];

 

  1. Partial MATLAB results

Variable values at the initial point

 t    = 0

     y     dy/dt        

     1    -1

     0     1

     0     0

 Solution for dependent variable y1

              t                  y1

            0            1

  5.0238e-005      0.99995

   0.00010048       0.9999

   0.00015071      0.99985

   0.00020095       0.9998

   0.00045214      0.99955

   0.00070333       0.9993

 

 MATLAB Plot Example