Conversion to struct from double is not possible.
조회 수: 2 (최근 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
답변 (3개)
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.
댓글 수: 0
Sulaymon Eshkabilov
2021년 11월 2일
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
2021년 11월 2일
Use elementwise operation:
...
xp=xp+xmin.*d(i,:); % Elementwise
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!