4 views (last 30 days)

Show older comments

Please load the“dataset.mat”file

(a). Develop first derivative function using Backward finite-difference

function [ diff_f ] = bdiff( f, x, h )

x_i = x;

x_ih = x-h;

diff_f = double((subs(f,x_i) - subs(f,x_ih)) / h);

end

(b). Calculate and display first derivative of given “velocity” data with respect to time

b-1. Unit of velocity is m/s

b-2. Set x-axis 0 to 30 and y-axis -5 to 5

b-3. Display should include differentiation of velocity from your function and “acceleration” data

(c). Caltulate the integration of velocity using Simpson's 1/3 rule ( 0sec to 30sec )

clear all

close all

clc

load('dataset.mat')

x = Time;

y = velocity;

y1 = acceleration;

h = Time(2) - Time(1);

N = 1000;

for i=2:N+1

bdiff_x = bdiff(y(i),x(i),h);

end

a = Time(1);

b = Time(2);

n = 30;

S = simpson(y,a,b,n);

figure

plot(x(1:N),bdiff_x,'b--','linewidth',1.5)

hold on

grid on

plot(x,y1,'g-','linewidth',1)

plot(x,S,'r--','linewidth',1)

legend('bDiff','Diff','S')

axis([0 30 -5 5])

xlabel('x')

ylabel('y')

I made a and solved B, but I can't even execute it. C couldn't do it. I need a help.

Jim Riggs
on 21 Dec 2019

Edited: Jim Riggs
on 21 Dec 2019

I am assuming that your question is that you don't understand Simpson's rule. Below is an exerpt from Wikipedia:

Simoson's Rule is a method of approximating the integral of a function. From the figure, above, Simpson'e rule approximates the integral of f(x) (the blue curve) by using a polynomial function shape (the red curve).

The botom equation shows the parameters for this equation. The method uses pairs of intervals (interval a to m, and m to b in the figure) . There must be an even number if intervals, therefore, there will be an odd number of data points.

Let's assume that your velocity data is in vector Y, which is n samples long (n needs to be an odd number). They also need to be evenly spaced. (interval a to m must be the same length as interval m to b on the independent axis).

You will add up approximations for each pair of intervals in Y, like so:

The first approximation consists of a pair of interval defined by a, m, and b; f(a) = Y(1) and f(m) = Y(2), f(b) = Y(3)

The area under the curve is given by the formula S = (h/3) * [ f(a) + 4 f(m) + f(b)], where h = (b-a)/2 , therefore

S1 = (h/3) * ( Y(1) + 4*Y(2) + Y(3) ); % this is the integral of Y from Y(1) to Y(3)

Now compute the area for the next 2 intervals:

S2 = (h/3) * ( Y(3) + 4*Y(4) + Y(5) ); % this is the integral of Y from Y(3) to Y(5)

The total area from Y(1) to Y(5) is: S = S1 + S2.

To generalize, you sum up the areas using interval pairs:

h = Time(2) - Time(1); % independent axis spacing (i.e. the time step)

n = length(Y); % n must be an odd number

S = 0;

for i=1:2:n % i will increment by 2 each pass through the loop

S = S + (h/3) * (Y(i) + 4*Y(i+1) + Y(i+2) );

end

The first pass through this loop, i=1 and the area increment = (h/3)*( Y(1) + 4*Y(2) + Y(3))

The second pass, i increments by 2 and is equal to 3, so the second increment is (h/3)*( Y(3) + 4*Y(4) + Y(5) ).

When the loop completes, S is the area under curveY from Y(1) to Y(n) (which is the distance traveled from Time(1) to Time(n).

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!