0% found this document useful (0 votes)
12 views22 pages

002 Nonlinear Equations

Nonlinear equation

Uploaded by

Amir Zidan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views22 pages

002 Nonlinear Equations

Nonlinear equation

Uploaded by

Amir Zidan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Dr. Hossam S.S.

AbdelMeguid
hssaleh@gmail.com
Mansoura University
2011-2012

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 1
• Roots or zeros of a function are the values of the
independent variable that will make the function value
equal to zero.
• For the quadratic function f ( x)  ax 2  bx  c  0
the solution is given by

 b  b 2  4ac
x
2a
This is an analytical solution.

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 2
• It is not possible to get an analytical solution to any
function:
x
f ( x)  e  x  0
has no analytical solution.

• One not so smart method is trial and error.


• Real vs complex roots

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 3
 Nonlinear algebraic equation: f(x) = 0
◦ Analytical solution rarely possible
◦ Need numerical techniques
◦ Multiples solutions may exist
 Iterative solution
◦ Start with an initial guess x0
◦ Algorithm generates x1 from x0
◦ Repeat to generate sequence x0, x1, x2, …
◦ Hope sequence convergences to solution
◦ Terminate algorithm at iteration N when:
 Many iterative algorithms available
◦ Fixed-point iteration, Newton-Raphson method, secant
method
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 4
• They give only a rough approximation of the root.
• They usually are used to start other methods.

Example 1
Find the root of:

f (c ) 
667.38
c

1  e 0.146843c  40 

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 5
Matlab code

c=-20:0.01:20
f=(667.38./c).*(1-exp(-0.146843 .*c))-40
plot(c,f)
600
grid on
xlabel('c'); 500

ylabel('f(c)') 400

300
f(c)

200
Root
100

-100
-20 -15 -10 -5 0 5 10 15 20
All Copycrights reserved
Dr.Hossam AbdelMeguid 18/09/2011 6
Example 2
Use the graphical approach to estimate the
x
root of the equation
e x0
Solution
4

3.5

Matlab code 2.5

x=-1:0.1:1 2

Root
y=f(x)

y=exp(-x)-x 1.5

1
plot(x,y) 0.5
grid on 0

xlabel('x'); -0.5

ylabel('y=f(x)') -1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 7
Find root of continuous function of one variable

Syntax

x = fzero (fun, x0)


Description
x = fzero(fun,x0) tries to find a zero of fun near x0, if x0 is a scalar. fun is a
function handle.
The value x returned by fzero is near a point where fun changes sign, or NaN
if the search fails. In this case, the search terminates when the search
interval is expanded until an Inf, NaN, or complex value is found.

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 8
 Solution of a single nonlinear algebraic equation:
1 1
f ( x)   6  0
( x  3)  0.01 ( x  0.9)  0.04
2 2

100

80

60
y=f(x)

40

20

-20
-2 -1 0 1 2 3 4 5
x
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 9
100

1 1
f ( x)   6  0
80

( x  3)  0.01 ( x  0.9)  0.04


2 2 60

y=f(x)
40

Create Matlab m-file function: fun.m:


20


0

function [ f ] = Fun_Ex3( x ) -20


-2 -1 0 1 2 3 4 5

f=1./((x-3).^2+0.01) + 1./((x-0.9).^2+0.04)-6;
x

end
 Call fzero from the Matlab command line to find the
solution:
>> x0=0; >> x0=0;
>>fzero(@Fun_Ex3,x0) >> fzero('Fun_Ex3',x0)
ans = ans =
0.5376 0.5376

 Different initial guesses, x0, can give different solutions:


>> x0=1; >> x0=2;
>> fzero('Fun_Ex3',x0) >> fzero('Fun_Ex3',x0)
ans = ans =
1.2694 2.5916
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 10
roots
Polynomial roots
Syntax
r = roots(c)
Description
r = roots(c) returns a column vector whose elements are the roots
of the polynomial c.

Row vector c contains the coefficients of a polynomial, ordered in


descending powers. If c has n+1 components, the polynomial it
represents is .
c1 xn  c2 x n1   cn x  cn1
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 11
Representing Polynomials:

x4 - 12x3 + 25x + 116


12000
>> P = [1 -12 0 25 116];
10000
>> roots (P)
8000
ans =
11.7473 6000
P

2.7028 4000

-1.2251 + 1.4672i
2000
-1.2251 - 1.4672i
0

-2000
-5 0 5 10 15
x
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 12
>>P = [1 -12 0 25 116];
>> r=roots(P)
r=
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i
>> PP=poly(r)
PP =
1.0000 -12.0000 -0.0000 25.0000 116.0000

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 13
fsolve
Solve system of nonlinear equations

Equation
Solves a problem specified by
F(x) = 0
for x, where x is a vector and F(x) is a function that returns
a vector value.

Syntax
x = fsolve(fun, x0)
x = fsolve(fun, x0, options)

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 14
Notation of the systems:
 f ( x , x ,... x )  0
 1 1 2 n - iterative methods (eq. (2), (3),
 f ( x1 , x 2 ,... x n)  0
 2  (1)  f (x T )  0 (2) - optimization methods,
 .......... ....... - Newton’s method
 f ( , ,... )  0
 n x1 x 2 x n

iterative methods Newton’s method


 x  ( x , x ,... x ) f  T
 1
n ( x )
1 1 2 n
f i ( x T)  f i x0 
T
 i 0
( x j  x0 j )  0
 x  ( x1 , x 2 ,... x n) Relaxation !
( )  (6)
j 1  x j
 2 2  (3)
 .................
J (xT T
 x  ( , ,... ) 0 ) x   f (x0 ) (7) x1  x 0  x  (9)
 n n x1 x 2 x n or

n
 f 1  f1  f 1  x1 j  x0 j  x j  (10)
xi  g i (x )   ci j (x T ) x j  (4)
T
 ... 
j 1   x1  x2  xn  j  1, 2,...n
 
i, j  1, 2,....n  f 2  f2  f 2
J   ...   (8)
  x1  x2  x n 

Optimization  ... ... ... ... 
 f  fn  f n 
n methods:  n
...
 f i ( x1 , x2 ,... xn)  min
2
(5) 
  x1  x2  Simplified approach
x n 
i 1
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 15
Solve the following system for x and y

f1 ( x, y)  x  4 x  xy  0
2

f 2 ( x, y)  2 y  y 2  3xy  0
1- Create an m-file Fun_Ex5
function [ f ] = Fun_Ex5( z )
% function for Example 5, Lecture 3

x=z(1);
y=z(2);

f(1)=x - 4*x.^2- x.*y;


f(2)=2*y- y.^2 + 3*x.*y;
end
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 16
2 – call the function fsolve to solve the system
>>x0 = [1; 1]; % Make a starting guess at the solution
>>options=optimset('Display','iter'); % Option to display output

>>[x,fval] = fsolve('Fun_Ex5',x0,options) % Call solver

x=
0.2500
0.0000

fval =
1.0e-007 *
-0.1631 0.0003
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 17
In the thermos the innermost compartment is
separated from the middle container by a
vacuum, There is a final shell around the
thermos , This final shell is separated from the
middle layer by a thin layer of air. The out side
of the final shell comes in contact with room air,
Heat transfer from the inner compartment to the next layer q1 is by
radiation only (since the space is evacuated), Heat transfer between
the middle layer and outside Shell q2 is by convection in a small
space , Heat transfer from the out side shell to the air q3 is by
natural convection, The heat flux from each region of the thermos
must be equal-that is, q1 = q2 = q3 Find the temperatures T1 and T2
at steady state , To = 450 C and T3 = 25 C.
q1  109 T0  273  T1  273 
4 4
 
q2  4 T1  T2 
q3  1.3 T2  T3 
4/3
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 18
1- Create an m-file Fun_Ex6
function [ f ] = Fun_Ex6( z )
% function for Example 6, Lecture 3
q=z(1); %q=q1=q2=q3
T0=450;
T1=z(2);
T2=z(3);
T3=25;

f(1)=1.0e-9*((T0+273).^4-(T1+273).^4)-q;
f(2)=4*(T1-T2)-q;
f(3)=1.3*(T2-T3).^(4/3)-q;
end
2 – call the function fsolve to solve the system
>>x0=[0.1 400 50];
>>options=optimset('Display','iter'); % Option to display output
>>[x,fval] = fsolve('Fun_Ex6',x0,options) % Call solver
q T1 T2
x = 244.9607 137.0988 75.8587
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 19
The matrix a is:
1,2,3
4,5,6
7,8,9

The vector b is:


10
11
12

The vector x is:


20
21
22

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 20
%%
a=[1 2 3 ; 4 5 6; 7 8 9];
b=[10 11 12];
x=[20 21 22];

fid=fopen('myFile.txt','w');
fprintf(fid,'The matrix a is: \r\n');
fclose (fid);
dlmwrite('myFile.txt',a,'-append','delimiter',',','newline','pc');

fid= fopen('myFile.txt','a');
fprintf(fid,'\r\nThe vector b is: \r\n');
fclose (fid);
dlmwrite('myFile.txt',b','-append','delimiter',',','newline','pc');

fid= fopen('myFile.txt','a');
fprintf(fid,'\r\nThe vector x is: \r\n');
fclose (fid);
dlmwrite('myFile.txt',x','-append','delimiter',',','newline','pc');

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 21
THE END

hssaleh@gmail.com

All Copy rights reserved


Dr.Hossam AbdelMeguid 18/09/2011 22

You might also like