Conversion to struct from double is not possible.

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 Commenti

Hi,
did you check the man page of fminsearch()?
[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.
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
i dont know how can i exaclty use fminsearch

Accedi per commentare.

Risposte (3)

Check
help fminsearch
The last output (called z in your script) is a struct. So line 16 doesn't make sense.
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 Commenti

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
x1=[1 2 3 4];
x0=xp;
for i=1:4
xmin= fminsearch(@funkcja1,x1);
xp=xp+xmin*d(i,:);
end
________________________________________________________________________________________
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the
number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in gauss (line 18)
xp=xp+xmin*d(i,:);
function f=funkcja(x)
f= 1484*x(1) - 252*x(2) - 168*x(1)*x(2) - 28*x(1)^2*x(2) + 757*x(1)^2 + 168*x(1)^3 + 14*x(2)^2 + 14*x(1)^4 + 1333;
here is that function
the size of the table does not match

Accedi per commentare.

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

Community Treasure Hunt

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

Start Hunting!

Translated by