Conversion to struct from double is not possible.

3 次查看(过去 30 天)
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 个评论
Michal Kopysc
Michal Kopysc 2021-11-2
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

请先登录,再进行评论。

回答(3 个)

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

Sulaymon Eshkabilov
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 个评论

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by