%
% - File name : nonlin_onevar_1.m (Generated by Hee Jung : fall/1999)
% - Solve nonlinear equations of one variable
% - Example problem : solve
%
% 0.1*e^(x) + sin(x) -5 = x
%
% for x.
% - Method 1 : Express equations in homogeneous form (i.e. f(x)=0) and plot
% x versus f(x) to see which values of x approximately satisfy the equations
% and zoom in the plot to find x more accurately. This method can not give
% exact solutions, but provides a rough idea what the solution is.
%
clear % removes all variables from the workspace
% Define x variables
x = 0:0.01:5; % 501 numbers betweeon zero and five.
% Define function f(x) as a list of numbers corresponding to x
f = 0.1*exp(x) + sin(x) - 5 - x;
% Draw the figure(1) : x versus f(x)
figure(1)
clf
plot(x,f)
xlabel('x'); ylabel('f(x) = 0.1*e^x + sin(x) - 5 - x');
title('(x) v.s. f(x) = 0.1*e^x + sin(x) - 5 - x');
grid on
% Zoom in the area where f(x) is close to zero, which is about at x=4.65
figure(2)
clf
plot(x,f)
axis([4.6,4.7, -1, 1]) % set the x axis interval
xlabel('x'); ylabel('f(x) = 0.1*e^x + sin(x) - 5 - x');
title('(x) v.s. f(x) : Zoom in ');
grid on
% From figure(2), the solution of this problem can be found as about x=4.670
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note : To apply this method, you may need some trial & error.
% If there's no solution in the x interval you specified,
% you need to try a different interval.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%