Steepest descent method n shows error 'Index exceeds matrix dimensions.', how to fix this
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
clc
clear all
%%%%%%%%%%%%%%%%Steepest Descent %%%%%%%%%%%%%%%5
ao=1;
bo=0.2;
co=0.1;
x = [0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8];
y= [1.196 1.379 1.581 1.79 2.013 2.279 2.545 2.842 3.173 3.5];
Sum=0
for i=1:10
S=((y(i)-(ao.*(exp(bo*x(i))+co*x(i)).^2)));
Sum=Sum+S
end
Initial_S=Sum
coeffi_mat=[1 0.2 0.1];
%now calculating next coefficints and then finding new S, till the value of S<0.01
j=1
while S>0.01
S_to_a=0;
S_to_b=0;
S_to_c=0;
for i=1:10
Part_1=(y(i)-(coeffi_mat(j,1)).*exp(coeffi_mat(j,2))*x(i));
S_to_a_sum_2=-1.*exp(coeffi_mat(j,2))*x(i);
S_to_a_sum= Part_1.*S_to_a_sum_2;
S_to_a= S_to_a+ S_to_a_sum;
S_to_b_sum_2=-(coeffi_mat(j,1)).*x(i).*exp(coeffi_mat(j,2))*x(i);
S_to_b_sum= Part_1.*S_to_b_sum_2;
S_to_c_sum_2=-x(i);
S_to_c=Part_1.*S_to_c_sum_2;
end
S_to_a_final=2.*S_to_a
S_to_b_final=2.*S_to_b
S_to_c_final=2.*S_to_c
%%%%%%%%%5calculating gradient%%%%%%%%5
grad=sqrt((S_to_a_final).^2+(S_to_b_final).^2+(S_to_c_final).^2)
%%%%%%%%%%%calculating unit vectoresb%%%%%%%%555
uv_a=S_to_a_final/grad
uv_b=S_to_b_final/grad
uv_c=S_to_c_final/grad
%%%%%%%%calculating new coefficient %%%%%%%
%%%del=0.02
a= coeffi_mat(i,1)-(0.02*uv_a)
b= coeffi_mat(i,2)-(0.02*uv_b)
c= coeffi_mat(i,3)-(0.02*uv_c)
%%%% Appending new coefficient in coefficient matrix %%%%%%%%%5
coeffi_mat=[coeffi_mat;a,b,c]
%%%%%%%% calculating the value of S %%%%%%%%
for i=1:10
S_1=y(i)
S_2= (coeffi_mat(i+1,1).*exp(coeffi_mat(j+1,2).*1)+coeffi_mat(j+2,3).*1)
S=((S_1)-(S_2))^2
end
end
%%when while loop will end, final value of S will be found then value of a,b,c will be expected as
final_a=coeffi_mat(end,1)
final_b=coeffi_mat(end,2)
final_c=coeffi_mat(end,3)
0 comentarios
Respuestas (1)
Cris LaPierre
el 4 de Oct. de 2020
The error means that somewhere in your code you are trying to access an element that doesn't exist. For example, if a variable has 10 values in it, you are trying to use the 11th.
When I run your code, the error appears here: a= coeffi_mat(i,1)-(0.02*uv_a)
Here, i has a value of 10 (final value from the for loop in lines 22-31), but coeffi_mat=[1 0.2 0.1]
2 comentarios
Cris LaPierre
el 4 de Oct. de 2020
Please share the full error message along with your code.
I see this message: Index in position 1 exceeds array bounds (must not exceed 2).
This is the same underliying cause as your first error. Specifically, it is this part of your equation (j=1):
coeffi_mat(j+2,3)
Ver también
Categorías
Más información sobre Elementary Math en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!