Conversion to struct from double is not possible.
Mostrar comentarios más antiguos
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 comentarios
Hi,
[x,fval,exitflag,output] = fminsearch(___)
The 4th ouput 'output' is a structure. Although I don't know what your code is supposed to do, the way you use the outputs of fminsearch (a,b,c,z) doesn't make much sense to me...
Also, you did not mention how you call your function, i.e. what is xp and epsilon?
Btw, formatting would make your question easier to read.
Michal Kopysc
el 2 de Nov. de 2021
Michal Kopysc
el 2 de Nov. de 2021
Respuestas (3)
Yongjian Feng
el 2 de Nov. de 2021
Check
help fminsearch
The last output (called z in your script) is a struct. So line 16 doesn't make sense.
Sulaymon Eshkabilov
el 2 de Nov. de 2021
0 votos
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 comentarios
Michal Kopysc
el 2 de Nov. de 2021
Michal Kopysc
el 2 de Nov. de 2021
Michal Kopysc
el 2 de Nov. de 2021
Michal Kopysc
el 2 de Nov. de 2021
Sulaymon Eshkabilov
el 2 de Nov. de 2021
Use elementwise operation:
...
xp=xp+xmin.*d(i,:); % Elementwise
end
Categorías
Más información sobre Formula Manipulation and Simplification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!