I don't understand my error
Show older comments
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)));
Answers (2)
Image Analyst
on 30 May 2022
0 votes
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.
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;
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!