0% found this document useful (0 votes)
10 views

Matlab non linear equations unit 4

Uploaded by

l50711876
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Matlab non linear equations unit 4

Uploaded by

l50711876
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Non- Linear Equations

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)

if x0 is given as 1 it won’t give the solution.

if x0 is given as 0.5 it displays the solution as 0.1586.(First Solution)

if x0 is given as 2 it displays the solution as 3.1462.(Second Solution)

if x0 is given as 5 it displays the solution as 3.1462.


Consider the Nonlinear Equations

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,

Lagrange Second Order Interpolation Formula


If the Degree of the polynomial is 2 then it is called Second Order Polynomial.
Lagrange Interpolation Formula for 2nd order polynomials is,
Example 1: Find the value of y at x = 2 for the given set of points (1, 2),(3, 4)
Solution:
Given,
(x0, y0) = (1, 2)
(x1, y1) = (3, 4)
First order Lagrange Interpolation Formula 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

f(x) or y = (2/3) + (20/3) = 22/3

The value of y at x = 5 is 22/3 or 7.33


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

f(x) or y = (2/3) + (20/3) = 22/3

The value of y at x = 5 is 22/3 or 7.33


Example 3: Find the value of y at x = 1 for the given set of points (1, 6), (3, 4),
(2, 5)
Solution:
Given, (x0, y0) = (1, 6) ,(x1, y1) = (3, 4) and (x2,y2) = (2,5)
Second order Lagrange Interpolation Formula is,

At x = 1

f(x) or y = (6) + (0) +(0) = 6

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

% Interpolation using Lagrange Polynomials - Matlab

%Part a - Connecting data points by intuition

xdata=[-1.000, -0.960, -0.860, -0.790, 0.220, 0.500, 0.930]; %experimental data


ydata=[-1.000, -0.151, 0.894, 0.986, 0.895, 0.500, -0.306]; %experimental data

%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')

%Part b - now we can try to interpolate the data using Lagrange


%Polynomials.

m=length(xdata); % m= n+1 i.e number of points is where n is degree.

%L is the function which will be used to find the approximating function.


%It is a matrix and is filled with 1s for multiplication purposes.
L=ones(m,length(x));
for k=1:m %the rows, i.e L1,L2, L3, L4....
for i=1:m %the columns L11, L12, L13....L17
if (k~=i) % if k not equal to i
L(k,:)=L(k,:).*((x-xdata(i))/(xdata(k)-xdata(i)));
end

end
end

y=0; % Using L polynomials to calculate the interpolating function.


for k=1:m
f=ydata(k).*L(k,:);
y=y+f
end
plot(x,y,'-g') %the interpolation polynomial is green
hold on
plot(xdata,ydata,'or')
xlabel('x');
ylabel('y');
title('Plot using Lagrange Polynomial Interpolation')
Straight Line fit,Cubic fit
using Least Squares method

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

f=input('Enter the function:');


df= input('Enter the derivative of the function:');
e=input('Enter the tolerance:');
x0=input('Enter the initial guess:');
n=input('Enter the no of iterations:');
%processing
if df(x0)~=0
for i=1:n
x1=x0-f(x0)/df(x0);
fprintf('x%d=%.20f\n',i,x1);
if abs(x1-x0)<e
break;
end
if df(x1)==0
disp('Newton Raphson failed');
end
x0=x1;
end
else
disp('Newton Raphson failed');
end

You might also like