Professional Documents
Culture Documents
Justin Dawber
September 22, 2011
Expectations/PreRequisites
• Simplest Examples
• Polynomials
• Switch to MatLab
nla_humps_comp.m
An Example: Finding Equilibrium
Concentrations
• Solution
• C_H2O = (C_H20)0 * (1-Xe)
• C_CO = (C_H20)0 * (1-Xe)
• C_H2 = (C_H20)0 * Xe
• C_CO2 = (C_H20)0 * Xe
• K = C_CO2*C_H2/(C_H20*C_CO)
Lets Try It:
• Switch to MatLab
nla_equilibrium_comp.m
2-Dimensional System of non-linear
equations
function main
clear all; close all; clc;
hold on
surf(X,Y,2*X - Y - exp(X) + 2) % first function
surf(X,Y,-X + 2*Y - exp(Y) + 2) % second function
zlabel('z')
view(69,8)
%%
initial_guesses = [1.3 0.9];
[X,fval,exitflag] = fsolve(@myfunc,initial_guesses)
function z = myfunc(X)
% set of equations to be solved
x = X(1);
y = X(2);
z = [2*x - y - exp(x) + 2;
-x + 2*y - exp(y) + 2];
Lets see it:
• Switch to MatLab
nla_1v_2d.m
An example: 2-Dimensional system
• Switch to MatLab
nla_2d.m
n-Dimensional Systems Example
• Given
• K1 = 25.82;
• K2 = 19.41;
• p = 1;
• nCf = 1; nH2f=0.5; nO2f = 0.5;
Lets see it:
• Switch to MatLab
nla_multidim.m
Thing to consider when using fsolve
• No Solutions
• Multiple Solutions
• Depends on the initial guess
• Translated HUMPS
• Let’s slide the HUMPS graph up 50
• It no longer crosses the X-axis
• We can attempt to solve it in the same way
• Switch to MatLab
nla_humps_translated_comp.m
Multiple Solution example
• As you can see, two Zeros. A guess around -.4 will return the
lower zero, while a guess near 1.2 will yield the high one.
Infinite Solutions Example
• Switch to Matlab
nla_2d_Coincidence.m
A little bit about Numerical Solvers -
Tolerances
• Call fsolve
• Guess is exactly right?!? - Something must be wrong
• Scaling is the problem
Tolerance Concern Example (cont.)
• How to Proceed?
• How to proceed?
• Switch to MatLab
Polynomials in MATLAB
• Defining Polynomials
• How to get f(x) = Ax2+Bx+C into MatLab
• Simple! --- >>f = [A B C]