Can't work out what I did wrong
이전 댓글 표시
I have obviously done something wrong with the f function I just dont know what. I apologise as this is most likely extreamly basic.
% compute the expressions f(x) and g(x)for x=10^-1,10^-2,...,10^-14
% which values (f(x) or g(x)is more accurate
x=[10^-(1),10^-(2),10^-(3),10^-(4),10^-(5),10^-(6),10^-(7),10^-(8),10^-(9),10^-(10),10^-(11),10^-(12),10^-(13),10^-(14)];
f=(exp(2.*x)-(exp(x).*cos(x)).^2./x.^2)
g=(exp(x).*sin(x)./x).^2
% modify program to compute relative error in the inaccurate values using
% the more accurate values as estimates of the true value. For each
% x=10^-1, 10^-2,...,10^-14, print x, computed values and relative error
error_f=(f(x)-g(x)/g(x))
댓글 수: 2
DGM
2021년 10월 3일
Since we're comparing f against g for some measure of accuracy, we need to know what they're supposed to be. The fact that they're vastly different means that one or both of the expressions is incorrect, but there's no way to know which.
There are a couple additional things that can't hurt:
x = 10.^-(1:14); % a simpler way to write that
f = (exp(2.*x)-(exp(x).*cos(x)).^2./x.^2)
g = (exp(x).*sin(x)./x).^2
error_f = (f-g)./g % f and g are just numeric vectors
I'm not sure that's the intended way to calculate the error, but that looks like what you were trying to do.
Tracy
2021년 10월 3일
채택된 답변
추가 답변 (2개)
KSSV
2021년 10월 3일
Replace the line
rror_f=(f(x)-g(x)/g(x)) ;
with
rror_f=(f-g./g) ;
f and g are already evaluated with the given values of x.
When you use f(x), g(x) as f, g are arrays, you are trying to index them with x which is not correct.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
