1 / 3100%
4/2/20 2:30 PM C:\Users\A...\engr133_lab10_KCariappa.txt 1 of 3
clear,clc,close all
clear,clc,close all
clear,clc,close all
clear,clc,close all
x = linspace(0,pi/2,10);
y = sin(x);
cosx = cos(x);
plot(x,cosx);grid minor
legend('d(sin(x)/dy = cos(x)')
title('Example of numerical differentiation')
%% Forward difference estimate of dy/dx.
y1 = diff(y)./diff(x);
n = length(x);
y1(n) = NaN;
hold on
plot(x,y1,'--*r')
legend('d(sin(x)/dy = cos(x)','forward difference')
%% Backward difference estimate of dy/dx.
y2 = diff(y)./diff(x);
y2 = [NaN y2];
plot(x,y2,'-.mp')
legend('d(sin(x)/dy = cos(x)','forward difference','backward difference')
%% Central difference estimate of dy/dx.
y3 = (y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2));
y3 = [NaN y3 NaN];
hold on
plot(x,y3,'o')
legend('d(sin(x)/dy = cos(x)','forward difference','backward difference','central
difference')
%% Gradient estimate of
y4=gradient(y,x)
y4 =
Columns 1 through 5
dy/dx.
0.9949 0.9798
0.9349
0.8616
0.7622
Columns 6 through 10
0.6395 0.4975
0.3403
0.1728
0.0870
plot(x,y4,‘+')
plot(x,y4,‘+')
{ Error: The input character is not valid in MATLAB
statements or expressions.
}
plot(x,y4,'+')
4/2/20 2:30 PM C:\Users\A...\engr133_lab10_KCariappa.txt 2 of 3
legend('d(sin(x)/dy = cos(x)','forward difference','backward difference','central
difference’,’gradient’)
legend('d(sin(x)/dy = cos(x)','forward difference','backward difference','central
difference’,’gradient’)
{ Error: Character vector is not terminated properly.
}
legend('d(sin(x)/dy = cos(x)','forward difference','backward difference','central
difference','gradient')
clc
%% polyder example
p1=[5, 2];p2=[10, 4, 3];
p1=[5, 2];p2=[10, 4, 3];
{ Error: The input character is not valid in MATLAB
statements or expressions.
}
p1=[5, 2];p2=[10, 4, -3];
% Derivative of p2.
der2 = polyder(p2)
der2 =
20 4
% Derivative of p1*p2.
prod = polyder(p1,p2)
prod =
150 80 -7
% Derivative of p2/p1.
[num, den] = polyder(p2,p1)
num =
50 40 23
den =
25 20 4
%% Plotting Rocket Launch data
% Import Time & Distance data from spreadsheet
% Create velocity vector from position data
Vel = gradient(Altitude,Time);
{ Undefined function or variable 'Altitude'.
4/2/20 2:30 PM C:\Users\A...\engr133_lab10_KCariappa.txt 3 of 3
}
Vel = gradient(Altitude,Time);
{ Undefined function or variable 'Altitude'.
}
Vel = gradient(Altitude,Time);
{ Undefined function or variable 'Altitude'.
}
diary off
Students also viewed