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.m367 chars16 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