Conversion to struct from double is not possible.

2 Ansichten (letzte 30 Tage)
Michal Kopysc
Michal Kopysc am 2 Nov. 2021
Hello
I have problem with my program
Here' the code
function f=funkcja1(x)
f= 8010+5*x(4)^2+40*x(2)*x(4)+40*x(1)*x(4)+6*x(3)^2+84*x(2)*x(3)+379*x(2)^2+310*x(1)*x(2)+1224*x(1)^2+2712*x(1)+10*x(4)-84*x(3)-418*x(2);
function gauss(xp,epsilon)
d=eye(4);
syms x1 x2 x3 x4 alfa;
fout=fopen('metgauss_przebieg_nowy.txt','w');
fprintf(fout,'Punkt poczatkowy: %10s %10s %10s %10s\r\n', num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)));
fprintf(fout,'%10s %43s %10s %10s\r\n','Iteracja','Punkt','f(x)','kryterium stopu');
fprintf(fout,'%10d %10s %10s %10s %10s %10s %10s\r\n',0,num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)),num2str(funkcja(xp)), '0');
tic
x0=xp;
for i=1:4
[a, b, c, z]= fminsearch(@funkcja1,[1 2 3 4]);
xp=xp+[a, b, c, z]*d(i,:);
end
iteracja=1;
while norm(xp-x0) >= epsilon
fprintf(fout,'%10d %10s %10s %10s %10s %10s %10s\r\n',iteracja,num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)),num2str(funkcja(xp)), num2str(norm(xp-x0)));
fprintf('Interacja=%d Kryterium stopu=%d\n',iteracja, norm(xp-x0) );
x0=xp;
for i=1:4
[a,b,c,z] = fminsearch(@funkcja1,[1 2 3 4]);
xp=xp+[a,b,c,z]*d(i,:);
end
iteracja=iteracja+1;
if iteracja==100
disp('Iteracja przekroczyla milion');
break
end
end
  3 Kommentare
Michal Kopysc
Michal Kopysc am 2 Nov. 2021
Sorry for my post but i am not good at that website so i copy paste my code, here is my all code, this program should search min of function
function f=funkcja1(x)
f= 8010+5*x(4)^2+40*x(2)*x(4)+40*x(1)*x(4)+6*x(3)^2+84*x(2)*x(3)+379*x(2)^2+310*x(1)*x(2)+1224*x(1)^2+2712*x(1)+10*x(4)-84*x(3)-418*x(2);
function gauss(xp,epsilon)
d=eye(4);
syms x1 x2 x3 x4 alfa;
fout=fopen('metgauss_przebieg_nowy.txt','w');
fprintf(fout,'Punkt poczatkowy: %10s %10s %10s %10s\r\n', num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)));
fprintf(fout,'%10s %43s %10s %10s\r\n','Iteracja','Punkt','f(x)','kryterium stopu');
fprintf(fout,'%10d %10s %10s %10s %10s %10s %10s\r\n',0,num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)),num2str(funkcja(xp)), '0');
tic
x0=xp;
for i=1:4
[a, b, c, z]= fminsearch(@funkcja1,[1 2 3 4]);
xp=xp+[a, b, c, z]*d(i,:);
end
iteracja=1;
while norm(xp-x0) >= epsilon
fprintf(fout,'%10d %10s %10s %10s %10s %10s %10s\r\n',iteracja,num2str(xp(1,1)),num2str(xp(1,2)),num2str(xp(1,3)),num2str(xp(1,4)),num2str(funkcja(xp)), num2str(norm(xp-x0)));
fprintf('Interacja=%d Kryterium stopu=%d\n',iteracja, norm(xp-x0) );
x0=xp;
for i=1:4
[a,b,c,z] = fminsearch(@funkcja1,[1 2 3 4]);
xp=xp+[a,b,c,z]*d(i,:);
end
iteracja=iteracja+1;
if iteracja==100
disp('Iteracja przekroczyla milion');
break
end
end
Michal Kopysc
Michal Kopysc am 2 Nov. 2021
i dont know how can i exaclty use fminsearch

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Yongjian Feng
Yongjian Feng am 2 Nov. 2021
Check
help fminsearch
The last output (called z in your script) is a struct. So line 16 doesn't make sense.

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 2 Nov. 2021
The error is coming from your problem exercise fcn @funkcja1. That would be helpful to see funkcja1 to give you proper hints how to fix a prompted error. Note that fminsearch() gives you four outputs, which are: [x,fval,exitflag,output]
x is found solutions
fval is the computed fcn values at x
exitflag is showing whether search suceeded or not (0 or 1)
output is a struct type of variable that contains: iterations, funcCount, algorithm, message. Where iterations are the nuber of interation, and funcCount is also number. And algorithm abd message are character strings. Thus, the way you are treating these are not correct. If you want to use the found solutions, then you should consider to use your variable "a" only.
See this documentation that explains all of these points in details: https://www.mathworks.com/help/matlab/ref/fminsearch.html
  4 Kommentare
Michal Kopysc
Michal Kopysc am 2 Nov. 2021
here is that function
Michal Kopysc
Michal Kopysc am 2 Nov. 2021
the size of the table does not match

Melden Sie sich an, um zu kommentieren.


Sulaymon Eshkabilov
Sulaymon Eshkabilov am 2 Nov. 2021
Use elementwise operation:
...
xp=xp+xmin.*d(i,:); % Elementwise
end

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by