 #1
 5
 0
The problem is: Compare the performance of the forward, backward, and central difference methods for estimating the derivative of the following function: y(x)=e^xsin(3x). Use 101 points from x = 0 to x = 4. Use a random additive error of +/ 0.01.
I understand the process of solving this one but I keep getting errors with how I enter my function, I still don't get all of the rules for those. What I am using is
y = exp(x)*sin(3x) I am guessing there's something wrong with this? Ill enter the rest of my entry to make it easier:
>> x = linspace(0,4,101);
n = length(x);
% The sinusoidal funciton
y = exp(x)*sin(3.x);
% added with measurment errors;
ym = y + 0.01*2*(rand(1,n)0.5);
% Note: rand(1,n) returns a vector of uniformly distributed random numbers
% between 0.0 and 1.0
figure(1);
plot(x,y,'k', x,ym,'bo');
xlabel('x');
ylabel('y');
legend('y=exp(x)*sin(3x)','meared signal with noise');
% The true derivative
dyt = exp(x)*sin(3x) + 3exp(x)*cos(3x);
% backward difference
dyb = diff(ym)./diff(x);
xb = x(2:n);
% forward difference
dyf = diff(ym)./diff(x);
xf = x(1:n1);
% central difference
dyc = gradient(ym)./gradient(x);
xc = x;
figure(2);
plot(x,dyt,'k');
hold on;
plot(xf, dyf, 'bo');
plot(xb, dyb, 'gs');
plot(xc, dyc, 'r^');
legend('true derivative','forward difference', 'backward','central');
xlabel('x');
ylabel('dy/dx');
? y = exp(x)*sin(3x);

Error: Unexpected MATLAB expression.
I understand the process of solving this one but I keep getting errors with how I enter my function, I still don't get all of the rules for those. What I am using is
y = exp(x)*sin(3x) I am guessing there's something wrong with this? Ill enter the rest of my entry to make it easier:
>> x = linspace(0,4,101);
n = length(x);
% The sinusoidal funciton
y = exp(x)*sin(3.x);
% added with measurment errors;
ym = y + 0.01*2*(rand(1,n)0.5);
% Note: rand(1,n) returns a vector of uniformly distributed random numbers
% between 0.0 and 1.0
figure(1);
plot(x,y,'k', x,ym,'bo');
xlabel('x');
ylabel('y');
legend('y=exp(x)*sin(3x)','meared signal with noise');
% The true derivative
dyt = exp(x)*sin(3x) + 3exp(x)*cos(3x);
% backward difference
dyb = diff(ym)./diff(x);
xb = x(2:n);
% forward difference
dyf = diff(ym)./diff(x);
xf = x(1:n1);
% central difference
dyc = gradient(ym)./gradient(x);
xc = x;
figure(2);
plot(x,dyt,'k');
hold on;
plot(xf, dyf, 'bo');
plot(xb, dyb, 'gs');
plot(xc, dyc, 'r^');
legend('true derivative','forward difference', 'backward','central');
xlabel('x');
ylabel('dy/dx');
? y = exp(x)*sin(3x);

Error: Unexpected MATLAB expression.