Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Steepest descent method , how to fix this error "Struct contents reference from a non-struct array object."

1 visualizzazione (ultimi 30 giorni)
ao=1;
bo=0.2;
co=0.1;
x = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
y= [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
Sum=0
for i=1:10
S=((y(i)-(ao.*(exp(bo.x(i))+co.x(i)).^2)));
fun = @(y,x) (a*(exp(bx))+c*x);
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
%%whn while loop will end, final value of Swill be found thn 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)

Risposte (2)

Walter Roberson
Walter Roberson il 3 Ott 2020
S=((y(i)-(ao.*(exp(bo.x(i))+co.x(i)).^2)));
Your bo and co are scalar constants. What does it mean to you to take the field named x of the scalar constant 0.2 and index that field at offset i?

Sarvjeet Singh
Sarvjeet Singh il 4 Ott 2020
error shows 'Index exceeds matrix dimensions.', how to fix
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)

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by