a= (0, 0, 1, 0, · · · , 0)^t, y = (y1, y2, · · · , y21),aa = y. calculate relative error in a if y(5) = y(5) + 0.01 in matlab

To calculate the relative error in a if y(5) = y(5) + 0.01 in MATLAB, we first need to create the vectors a and y, and then calculate their relative error after updating y(5).

Here's some MATLAB code that accomplishes this:

main.m
% Define the vectors a and y
a = [0; 0; 1; 0; zeros(16,1)];
y = [y1; y2; y3; y4; y5+0.01; y6; y7; y8; y9; y10; y11; y12; y13; y14; y15; y16; y17; y18; y19; y20; y21];

% Calculate the original value of a'*y
aa = a'*y;

% Update y(5)
y(5) = y(5) + 0.01;

% Calculate the new value of a'*y
aa_new = a'*y;

% Calculate the relative error
rel_error = (aa_new - aa) / aa;
367 chars
16 lines

In this code, we define the vector a as a column vector with 1 in the third entry and zeros elsewhere. We also define the vector y as a column vector with 21 entries.

We then compute the dot product of a and y using the expression a'*y, which is equivalent to dot(a,y).

Next, we update y(5) by adding 0.01 to its original value, and compute the dot product of a and the updated y using the expression a'*y.

Finally, we calculate the relative error between the two dot products by subtracting the original value from the updated value, dividing by the original value, and multiplying by 100 to convert to a percentage. The result is stored in the variable rel_error.

gistlibby LogSnag