002 Nonlinear Equations
002 Nonlinear Equations
AbdelMeguid
hssaleh@gmail.com
Mansoura University
2011-2012
b b 2 4ac
x
2a
This is an analytical solution.
Example 1
Find the root of:
f (c )
667.38
c
1 e 0.146843c 40
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 x0
Solution
4
3.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
Syntax
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
y=f(x)
40
0
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
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
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)
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);
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 109 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
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');
hssaleh@gmail.com