0% found this document useful (0 votes)
324 views6 pages

Geng 4

The document discusses using numerical integration methods to calculate the distance traveled by a car decelerating from 50m/s to 25m/s. 1) The trapezoidal rule and Simpson's rule were used in MATLAB with 16, 32, and 128 intervals, getting distances of 121.38m, 121.28m, and 121.27m respectively with trapezoidal and 106.73m, 114.22m, and 119.49m respectively with Simpson's rule. 2) By hand, the area under the curve was evaluated to be 127.71m, showing the numerical methods were approximations that did not account for the full curve. The trapezoidal rule was more accurate.

Uploaded by

justdaksh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
324 views6 pages

Geng 4

The document discusses using numerical integration methods to calculate the distance traveled by a car decelerating from 50m/s to 25m/s. 1) The trapezoidal rule and Simpson's rule were used in MATLAB with 16, 32, and 128 intervals, getting distances of 121.38m, 121.28m, and 121.27m respectively with trapezoidal and 106.73m, 114.22m, and 119.49m respectively with Simpson's rule. 2) By hand, the area under the curve was evaluated to be 127.71m, showing the numerical methods were approximations that did not account for the full curve. The trapezoidal rule was more accurate.

Uploaded by

justdaksh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Geng2140 Assignment 4

Q1)

a)

The first two iterations using the golden section ratio for the equation
f(t) = 30*t.^4 - 20*t.^3 - sqrt(abs(2 - sqrt(1 + 9*t.^3 + 1./(t + 0.15)))) +1
knowing that the root occurs between x = 0 and x = 0.5 are as follows.

Xs Xl Xo Xn f g

0 0.5000 0.3090 0.1910 0.3037 0.8666 for n = 0

0 0.3090 0.1910 0.1180 0.3037 0.8666 for n= 1

0.1180 0.3090 0.2361 0.1910 0.8666 0.5505 for n = 2

Please see the hand drawn interval attached.

b)

We can use the following matlab function to determine the root of the equation
from part a, with an error < 0.0001, and after how many iterations we can find
this root.

function [maxfvalue,tvalue,Niters] = goldensection(e)


xs = 0; %small value of x chosen
xl = 0.5; %large value of x chosen
nite = 0
d = ((sqrt(5) - 1)/2)*(xl-xs); %golden section rule
xo = xs + d;
xn = xl - d;

while (1)
f = 30*(xo).^4 - 20*(xo).^3 - sqrt(abs(2 - sqrt(1 + 9*(xo).^3 + 1./((xo) +
0.15)))) +1;
g = 30*(xn).^4 - 20*(xn).^3 - sqrt(abs(2 - sqrt(1 + 9*(xn).^3 + 1./((xn) +
0.15)))) +1;
nite = nite + 1;

if (abs(xo - xn)/2) < 0.0001


disp(sprintf('The tolerance was reached after %d niterations at max =
%0.4f', nite, xo))
return
end

if f>g;
xs = xn;
d = ((sqrt(5) - 1)/2)*(xl-xs);
xn = xo
xo = xs + d

else g>f;
xl = xo
d = ((sqrt(5) - 1)/2)*(xl-xs);
xo = xn
xn = xl - d
end
end

This matlab function returns a maximum value after 15 niterations, at a


maximum of x = 0.1904, suggesting the root occurs at x = 0.1904. A table of the
values are as follows.

Xs Xl Xo Xn f g

0 0.5000 0.3090 0.1910 0.3037 0.8666 for n = 0

0 0.3090 0.1910 0.1180 0.3037 0.8666 for n= 1

0.1180 0.3090 0.2361 0.1910 0.8666 0.5505 for n = 2

0.1180 0.2361 0.1910 0.1631 0.5576 0.8666 for n = 3

0.1631 0.2361 0.2082 0.1910 0.8666 0.6949 for n = 4

0.1631 0.2082 0.1910 0.1803 0.6969 0.8666 for n = 5

0.1803 0.2082 0.1976 0.1910 0.8666 0.7734 for n = 6

0.1803 0.1976 0.1910 0.1869 0.7757 0.8666 for n = 7

0.1869 0.1976 0.1935 0.1910 0.8666 0.8241 for n = 8

0.1869 0.1935 0.1910 0.1894 0.8204 0.8666 for n = 9

0.1894 0.1935 0.1919 0.1910 0.8666 0.8600 for n = 10

0.1894 0.1919 0.1910 0.1904 0.8447 0.8666 for n = 11

0.1894 0.1910 0.1904 0.1900 0.8666 0.8969 for n = 12

0.1900 0.1910 0.1906 0.1904 0.8969 0.8758 for n = 13

0.1900 0.1906 0.1904 0.1902 0.8798 0.8969 for n = 14

These results show that even over such a small interval, quite a few iterations
were required to determine the root with such a small amount of error. The first
11 runs of this function showed the biggest difference between the low value of x
chosen and the high value of x chosen, with the low value moving to 0.1894 and
the high value moving to 0.1935, leaving a range of just 0.041. This suggests
that, when using the golden section by hand, a smaller interval should be used
for a more time efficient method, or a larger error should be allowed for.
Q2)

Please see attached working.

The Lagrange Polynomial produced using the original starting points =


0.382x^2 – 2.400x + 2.828

The Lagrange Polynomial after one iteration is = 0.428x^2 – 2.7285x + 3.3431

Using parabolic interpolation, the minimum for this original polynomial is


3.14142 for the interval of pi/2 to 3*pi/2. This makes sense, as this roughly
equals pi, and for the graph of g(x) = cos(x), the minimum occurs at x = pi, with
a minimum of -1. Substituting x = 3.14142 into the Lagrange polynomial, we get
a minimum of -0.9416, giving an error of about 5%.

Using the second Lagrange Polynomial, we get a minimum at a value of x =


3.1838, which is only slightly higher than the real value. A graph of these
Lagrange polynomials is as follows.

0.2

-0.2

-0.4

-0.6

-0.8

-1

-1.2
1.5 2 2.5 3 3.5 4 4.5 5

Q3)

We are trying to find out the distance a car travels as it decelerates from 50m/s
to 25m/s, using the equation 2000*u(du/dx) = - 10*(u.^2+100).*(1+1./exp((u-30).^2)) to
illustrate the cruising of the car.
a)

Using the trapezoidal rule in matlab, for 16, 32 and 128 iterations, we can
determine the distance travelled by the car.

function dist = trapezoidal(finalSpeed, Nitervals)


runs = 0;
for k = 1:3
if(runs == 0)
interval = 16;
runs = 1; % for the first run, use 16 intervals
elseif (runs == 1)
interval = 32;
runs = 2; % for the second run, use 32 intervals
elseif runs == 2
interval = 128; % for the third run, use 128 intervals
end

UArray = 50:-25/interval:25; %between 50 and 25, using the designated


number of intervals
fa = (2000*50)./(-10.*(50.^2+100).*(1+1./exp((50-30).^2))); %force at 50
km/h
fb = (2000*25)./(-10.*(25.^2+100).*(1+1./exp((25-30).^2))); %force at 25
km/h
x = 0;

for n = 1:interval-1
x = x + 1;
u = (50+x*((25-50)/interval)); %the velocity at each interval
A(x) = (2000*u)./(-10.*(u.^2+100).*(1+1./exp((u-30).^2)));
end

D = ((25-50)/interval)*((fa+fb)/2 + sum(A)); %the sum of the distances


traveled per interval

disp(sprintf('For %d iterations, the car travels %.2f meters with a speed


decreasing from 50m/s to 25m/s',interval,D));

end

For 16 iterations, the car travels 121.38 meters with a speed decreasing from
50m/s to 25m/s

For 32 iterations, the car travels 121.28 meters with a speed decreasing from
50m/s to 25m/s

For 128 iterations, the car travels 121.27 meters with a speed decreasing from
50m/s to 25m/s

b)

We can retry this using the simpson rule.

function dist = simpson(finalSpeed, Nitervals)


runs = 0;
for k = 1:3
if(runs == 0)
interval = 16; %for the first run, use 16 intervals
runs = 1;

elseif (runs == 1)
interval = 32; %for the second run, use 32 intervals
runs = 2;

elseif runs == 2
interval = 128; %for the third run, use 128 intervals

end

fa = (2000*50)./(-10.*(50.^2+100).*(1+1./exp((50-30).^2))); %force at 50
km/h
fb = (2000*25)./(-10.*(25.^2+100).*(1+1./exp((25-30).^2))); %force at 25
km/h
x = 0

for n = 1:((interval/2)-1)
x = x + 2;
u = (50+(x-1)*((25-50)/interval));
r = (50+x*((25-50)/interval));
A(x) = (2000*u)./(-10.*(u.^2+100).*(1+1./exp((u-30).^2)));
B(x) = (2000*r)./(-10.*(r.^2+100).*(1+1./exp((r-30).^2)));

end

D = (25-50)*((fb + 4*sum(A) + 2*sum(B) + fa))/(3*interval); %sum of the


distances over the intervals

disp(sprintf('For %d iterations, the car travels %.2f meters with a speed


decreasing from 50m/s to 25m/s',interval,D));

end

For 16 iterations, the car travels 106.73 meters with a speed decreasing from
50m/s to 25m/s

For 32 iterations, the car travels 114.22 meters with a speed decreasing from
50m/s to 25m/s

For 128 iterations, the car travels 119.49 meters with a speed decreasing from
50m/s to 25m/s

c)

Please see attached working.

By hand, we evaluate the area under the curve to equal 127.71, meaning that
the car travelled 127.71m as it slowed down from 50m/s to 25m/s.
1020

1000

980

960

940

920

900

880
25 30 35 40 45 50

d) The value generated by hand was quite a bit larger than the values generated
by matlab using the trapezoidal and simpson rules. This makes sense, as the
trapezoidal rule and simpson rule are approximations, and would cut out part of
the curve. It also shows that the trapezoidal rule is generating answers closer to
the real value of the distance, and is more accurate in this case.

Katherine Chapman

20268352

You might also like