MATLAB: Relative error: Error using ==> mtimes

error

Hello I am trying to get the relative error for sinx=sqrt(1-cos^2(x) and getting error with this matlab code:
clear
clc
x=logspace(-1,-10,10);
lhs=sin(x);
t=cos(x);
rhs=sqrt(1-cos(x)*cos(x));
abserr=abs(lhs-rhs);
Rel=abs((lhs-rhs)/lhs);
disp('x error rel.error')
disp('--------------------')
for i=1:10
disp(sprintf('%22.15e %22.15e %22.15e\n',x(i),abserr(i),Rel(i)))
end
end
??? Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> Q at 6 rhs=sqrt(1-cos(x)*cos(x));
any idea? thanks

Best Answer

rhs = sqrt(1 - cos(x) .* cos(x))
While * performs the matrix multiplication, .* multiplies elementwise.
Related Question