Matlab Code
Matlab Code
Labwork 5
BY
Semester : 7
SCHOOL OF TECHNOLOGY
Theory:
This process may be repeated as many times as necessary to get the desired accuracy. In general, for
any x-value xn, the next value is given by
𝑓(𝑥𝑛)
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′(𝑥 ) 𝑛
Geometric Representation
We draw a tangent line to the graph of f(x) at the point x = xn. This line has slope f'(xn) and goes
through the point (xn, f(xn)). Therefore, it has the equation y=f’(xn)(x- xn) + f(xn). Now, we find the
root of this tangent line by setting y = 0 and x= xn +1 for our new approximation. Solving this
equation gives us our new approximation, which is
𝑓(𝑥𝑛)
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′(𝑥 ) 𝑛
2
Smit thummar 19BCH060
F(x) = ex-x3.
Use Newton Raphson method to find the root of this function using two initial guess values (i) x0=0
and (ii) x0=1.5. Write your observations
Code:
1) Take initial value x0=0
clc
clear all
x1=0; %Initial guess value
x(1)=x1;
for i=1:15
y(i)=exp(x(i))-(x(i)^3);
yder(i)=exp(x(i))-3*(x(i)^2);
x(i+1)=x(i)-(y(i)/yder(i));
end
x
x =
Columns 1 through 17
Results:
3
Smit thummar 19BCH060
It was observed that as we change the value of the initial value the root obtained was changed so it
was the limitation. To find all the roots using matlab we should have curve of the function so that we
can guess initial values for root finding as we have to do more initial guess to find all the solution.
Also it was found that is root is near to the guess value than less iterations are required.
4
Smit thummar 19BCH060
F(x) = ex-x3.
Use Newton Raphson method to find the optimum of this function using three initial guess values (i)
x0=0 and (ii) x0=1 and (iii) x0=10. Write your observations.
Code:
1. x0=0
clc
clear all
x1=0;%Initial guess value
x(1)=x1;
for i=1:6
y(i)=exp(x(i))-(x(i)^3);
yder(i)=exp(x(i))-3*(x(i)^2);
y2der(i)=exp(x(i))-(6*x(i));
x(i+1)=x(i)-(yder(i)/y2der(i));
error(i)=(x(i+1)-x(i))/x(i)
end
x
2. x0=1
code:
clc
clear all
x1=1;%Initial guess value
x(1)=x1;
for i=1:5
y(i)=exp(x(i))-(x(i)^3);
yder(i)=exp(x(i))-3*(x(i)^2);
y2der(i)=exp(x(i))-(6*x(i));
x(i+1)=x(i)-(yder(i)/y2der(i));
error(i)=(x(i+1)-x(i))/x(i)
end
x
x =
3. x0=1
code:
clc
clear all
x1=10;%Initial guess value
x(1)=x1;
for i=1:15
y(i)=exp(x(i))-(x(i)^3);
yder(i)=exp(x(i))-3*(x(i)^2);
y2der(i)=exp(x(i))-(6*x(i));
x(i+1)=x(i)-(yder(i)/y2der(i));
error(i)=(x(i+1)-x(i))/x(i)
end
x
5
Smit thummar 19BCH060
x =
Columns 1 through 13
Columns 14 through 16
Results:
It was observed that as we change the value of the initial value the optimum value obtained was
changed. To find optimum value using matlab we should have curve of the function so that we can
guess initial values for optimum value finding as we have to do more initial guess to find optimum
value as we are unaware that where optima of this function lies.
6
Smit thummar 19BCH060
F(x) = ex-x3.
Use bisection method to find the optimum of this function in the intervals of (i) [2 5] and (ii) [-2 5].
Write your observations.
Theory:
1. For range [2 5]
Code:
clc
clear all
a=2; b=5; %Bound
for i=1:20
c=(b+a)/2;
ydera=exp(a)-3*((a)^2)
yderb=exp(b)-3*((b)^2)
yderc=exp(c)-3*((c)^2)
if ydera*yderc<0
b=c
else
a=c
end
updatedc(i)=c;
end
updatedc
updatedc =
Columns 1 through 13
Columns 14 through 20
7
Smit thummar 19BCH060
updatedc =
Columns 1 through 13
Columns 14 through 20
Results:
It was observed that as we change the value of the bound the optimum value obtained was remain
unchanged as the optimum value lies in that bound. If we change bound to [2 3] than we will get
optima 3 as opima of the function is not included in our guess. To find optimum values using matlab
we should have curve of the function so that we can guess bound for finding optima.