Matlab non linear equations unit 4
Matlab non linear equations unit 4
3-1
Solving Non-linear Equations
using fsolve
3-2
Consider the Nonlinear Equation f(x)=2-x+ln(x)
The maximum value of f(x) is 1,To solve using fsolve, initial value must be guessed
and it cannot be local maximum or minimum.
Matlab Program
xsol=fsolve(@(x) 2-x+log(x),x0)
In order to solve this system, we first need to define a MATLAB function that
returns the value of the left-hand side of (1). The function is given below
function F=nonlinear_function(x)
F=[2*x(1)-x(2)-exp(-x(1));
-x(1)+2*x(2)-exp(-x(2))];
end
For a given value of the vector x=[x(1); x(2)], this function computes the left-hand
side of the system (1)
clc;
clear all;
% Possible algorithms 'trust-region-dogleg', 'trust-region', or 'levenberg-marquardt'
options = optimoptions(@fsolve,'Algorithm','trust-
region','Display','iter','UseParallel',true,'OptimalityTolerance',1.0000e-8)
initial_guess=[0;0]
[solution] = fsolve(@(x)nonlinear_function(x),initial_guess,options);
F=[2*solution(1)-solution(2)-exp(-solution(1));
-solution(1)+2*solution(2)-exp(-solution(2))];
Output
function fval=lorenzSystem(X)
%Define three Variables
x=X(1);
y=X(2);
z=X(3);
%Define f(x)
fval(1,1)=x-y;
fval(2,1)=2*x-x*z-y;
fval(3,1)=x*y-3*z;
end
x0=[1;1;1];
xsol=fsolve(@(x) lorenzSystem(x),x0);
disp(‘Solution is:’);
disp(xsol);
Output: if x0=[1;1;1] and also x0=[-1;-1,1] give same solution
if x0=[-1;-1;0]
Lagrange Interpolation –
1-Dimension & 2-Dimension
3-11
Lagrange Interpolation:
Lagrange Interpolation is a way of finding the value of any function at any given point when
the function is not given. We use other points on the function to get the value of the
function at any required point.
Suppose we have a function y = f(x) in which substituting the values of x gives different
values of y. And we are given two points (x1, y1) and (x2, y2) on the curve then the value of
y at x = a(constant) is calculated using Lagrange Interpolation Formula.
Lagrange First Order Interpolation Formula
If the Degree of the polynomial is 1 then it is called the First Order Polynomial.
Lagrange Interpolation Formula for 1st order polynomials is,
At x = 2
f(x) or y = 1 + 2 = 3
The value of y at x = 2 is 3
Example 2: Find the value of y at x = 5 for the given set of points (9, 2),
(3, 10)
Solution:
Given, (x0, y0) = (9, 2) and (x1, y1) = (3, 10)
First order Lagrange Interpolation Formula is,
At x = 5
At x = 5
At x = 1
The value of y at x = 1 is 6
MATLAB Program-1
X=input('Enter the list of abscissa:');
Y=input('Enter the list of ordinates:');
n=length(X);
L=zeros(n,n);
for i=1:n %for rows
V=1;
for j=1:n %for making polynomial
if i~=j
V=conv(V,poly(X(j)))/(X(i)-X(j));
end
end
L(i,:)=V*Y(i);
end
L
V
P=sum(L)
MATLAB Program-2
% This code interpolates given data points using Lagrange Polynomials. Part a simply joins the
points (which is %not accurate as a function at all), and part b interpolates the points to get a
function
%when plotting my polynomial I want to use x values from -1 to 0.93 with %1000 divisions. The
variables %below are simply used to generate 1000
%values of x in order to plot.
dx=(0.930+1)/1000;
x=(-1.00:dx:0.930);
%plot(xdata,ydata,'or') %PLOT THE POINTS IN RED
%hold on
%plot(xdata,ydata) % PLOT THE INTUITIVE INTERPOLATING LINE in blue
xlabel('x');
ylabel('y');
title('Plot using intuition')
end
end
3-29
%Curve fitting program
X=input('Enter the list of abscissa:');
Y=input('Enter the list of ordinates:');
F=input('Enter 1 for Linear fit, 2 for Parabola fit and so on :');
n=length(X);
N=F+1;
A=zeros(N,N);
for i=1:N
for j=1:N
A(i,j)=sum(X.^(i+j-2));
end
end
B=zeros(N,1);
for k=1:N
B(k)=sum((X.^(k-1)).*Y);
end
U=A\B
disp('Your Polynomial is:');
for k=N:-1:2
fprintf('+(%.4fx^%d)',U(k),k-1);
end
fprintf('+(%.4f)\n',U(1));
%plotting
P=flip(U);
x=linspace(X(1),X(n),100);
y=polyval(P,x);
plot(x,y,'r');
hold on
plot(X,Y,'o');
Curve fitting using polyval
and polyfit commands
3-37
%Curve fitting using polyval and polyfit commands
x=0:0.2:1;
y=[-0.45 3.2 6.2 7.5 9.5 11.4];
p=polyfit(x,y,1);
y_fit=polyval(p,x);
plot(x,y,'ro',x,y_fit);
% Finding the roots of the polynomial x^3-3x^2+4x-2=0
p=[1 -3 4 -2];
r=roots(p);
disp(‘Roots of the given polynomial’);
disp(r);
% Finding the roots of the nonlinear function x=e^(-ax) ,a=1/2
f=@(x,a) x-exp(-a*x);
a=1/2;
x0=0;
r=fzero(@(x) f(x,a),x0);
disp(‘Roots of the given nonlinear function’);
disp(r);
% Finding the roots of the nonlinear function using Newton Raphson method