close all
clear
clc
osf = 14.621;
Y = log(osf);
T = 0 : 5 : 40;
Ta = T + 273.15;
y=@(Ta) -139.34411 + (1.575701*10^5)./Ta - (6.642308*10^7)./Ta.^2 + (1.243800*10^10)./Ta.^3 - (8.621949*10^11)./Ta.^4 - Y;
g = diff(y(Ta));
Xn_0 = 274;
es = 0.5;
maxiteration = 100;
Xn = Xn_0 - ((y(Xn_0))/(g(Xn_0)));
ea = (((Xn - Xn_0)/(Xn))*100);
disp('Xn_0 y(Xn_0) g(Xn_0) Xn')
disp(num2str([Xn_0 y(Xn_0) g(Xn_0) Xn]));
flag = 1;
while abs (ea)>es
Xn_0 = Xn;
Xn = Xn_0 - y(Xn_0)/g(Xn_0);
ea = (((Xn - Xn_0)*100)/Xn);
disp(num2str([Xn_0 y(Xn_0) g(Xn_0) Xn ea]));
flag = flag+1;
if(flag == maxiteration)
break;
end
end
THE ERROR:
Index exceeds the number of array elements (8).
Error in Coding5 (line 17)
Xn = Xn_0 - ((y(Xn_0))/(g(Xn_0)));

답변 (2개)

Image Analyst
Image Analyst 2022년 5월 30일

0 개 추천

Name Size Bytes Class Attributes
y 1x1 32 function_handle
g 1x8 64 double
So y has 1 element and g has 8 elements. You're trying to get the 274'th element of each of these. Why?
You can't do that. Re-think what you want to do.
Jan
Jan 2022년 5월 30일
편집: Jan 2022년 5월 30일

0 개 추천

y = @(Ta) -139.34411 + 1.575701e5 ./ Ta - 6.642308e7 ./ Ta.^2 + 1.243800e10 ./ Ta.^3 - ...
8.621949e11 ./ Ta.^4 - Y;
g = diff(y(Ta));
No g is not the function, which is the derivative of y, but a vector containing the differences between the neighbors. Then:
Xn = Xn_0 - y(Xn_0) / g(Xn_0); % Avoid overdoing of parentheses
call the vector g with the index Xn_0.
I guess you want this:
% First derivative of y:
g = @(Ta) 1.575701e5 - 2 * 6.642308e7 ./ Ta + 3 * 1.243800e10 ./ Ta.^2 - ...
4 * 8.621949e11 ./ Ta.^3;

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2022년 5월 30일

편집:

Jan
2022년 5월 30일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by