Your objective function must return a scalar value.
Show older comments
Dear Friends,
I am facing with the problem in my optimization " Your objective function must return a scalar value"
I write my function name in my optimization part of my matlab, ga or fmincon or others. that function read another function also.
a copy of them are attached.
Thanks and a dvice
M.H.
Accepted Answer
More Answers (26)
Walter Roberson
on 19 Dec 2019
Omega=10:10000:4e6;
% % % % x=0.1;
for i=1:1:400
finalresp(i)=abs((TransOpt(x,Omega(i))-Z(i))^2);
end
You should be pre-allocating finalresp.
It would be better not to assume that Omega is exactly length 400, and to loop to length(Omega) instead of to 400.
y=(finalresp);
Well, that is obviously going to be a vector result, of length 400. ga() cannot process a vector result. gamultiobj() can process a vector result -- but do you really want to optimize for the pareto fronts of finalresp, or do you instead want to optimize some property of finalresp() as a whole, such as sum(finalresp) ?
If you are going for minimum residue, then I recommend that you switch to
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
finalresp(i)=(TransOpt(x,Omega(i))-Z(i))^2;
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
because abs() is not differentiable .
Exception: if Z can be complex valued, then
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
temp = (TransOpt(x,Omega(i))-Z(i))^2;
finalresp(i) = temp .* conj(temp);
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
load('Z.mat')
We do not have Z.mat to test with.
Omega=10:10000:4e6;
% % % % x=0.1;
for i=1:1:400
finalresp(i)=abs((TransOpt(x,Omega(i))-Z(i))^2);
end
You should be pre-allocating finalresp.
It would be better not to assume that Omega is exactly length 400, and to loop to length(Omega) instead of to 400.
y=(finalresp);
Well, that is obviously going to be a vector result, of length 400. ga() cannot process a vector result. gamultiobj() can process a vector result -- but do you really want to optimize for the pareto fronts of finalresp, or do you instead want to optimize some property of finalresp() as a whole, such as sum(finalresp) ?
If you are going for minimum residue, then I recommend that you switch to
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
finalresp(i)=(TransOpt(x,Omega(i))-Z(i))^2;
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
because abs() is not differentiable .
Exception: if Z can be complex valued, then
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
temp = (TransOpt(x,Omega(i))-Z(i))^2;
finalresp(i) = temp .* conj(temp);
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
1 Comment
Walter Roberson
on 19 Dec 2019
You have precision problems. For your second Omega, 10010, rank(T) is only 6 when T is calculated in double precision. If you switch to symbolic calculations, then you get enough extra precision for full rank -- that is, vpa(T,16)\Y does not give precision problems.
Ideally you would calculate the entire function in symbolic form, but I find that as long as you calculate
Z = double(vpa(T,16)\Y)
then that is close enough for double precision purposes -- carrying out the whole calculation in symbolic form and using vpa(T,32) gives a result that differs by only about 1E-14
mohammad hodaei
on 19 Dec 2019
0 votes
2 Comments
Walter Roberson
on 19 Dec 2019
Your code ignores all x except the first in the array. Your code always indexes x(1) and never any other index of x.
mohammad hodaei
on 19 Dec 2019
mohammad hodaei
on 19 Dec 2019
0 votes
1 Comment
Walter Roberson
on 19 Dec 2019
Your code already does
HodaInverse=abs(WZ(6))
so there is no possibility that it will return a complex number.
finalresp(i)=abs(TransOpt(x,Omega(i))-Z95(i))^2 ;
Your TransOpt() result will be real-valued, and your Z95 are all real-valued, so abs() of the subtraction squared is the same as just squaring the subtraction numerically. However, for theoretical reasons, abs() is not differentiable as required by most of the optimization algorithms (except ga(), ga() does not care.) It is therefore better to remove the abs() step -- and it should be slightly more efficient too.
With the abs() removed, what you are constructing is a sum-of-squared-residues, which turns the problem into a nonlinear least-squared problem.
mohammad hodaei
on 19 Dec 2019
0 votes
1 Comment
Walter Roberson
on 19 Dec 2019
We are not given enough information about how you changed your calculations to use x(2), x(3) as well, and we are not given enough information to know what the expected values are so that we could judge whether there is a discrepency or not.
mohammad hodaei
on 19 Dec 2019
0 votes
1 Comment
Walter Roberson
on 20 Dec 2019
I need the version of the function that uses x(2) and x(3)
mohammad hodaei
on 20 Dec 2019
0 votes
2 Comments
Walter Roberson
on 22 Dec 2019
I created a version of TransOpt that is vectorized in Omega, to reduce the calculation overhead. However, it is still not fast. My custom minimizer has been working on it for the last day solid -- and I didn't even configure it to do a fine search.
Right at the moment, my best results are with [1.17989735119807637, 1.99095000884609474, 0.049245285489269458, 2609240480.47521973] but I have not even had a chance yet to test with the [0.947 0.001 1.027 0.50087e9] that you had found.
I am also working on a version that is vectorized in both Omega and x, but I need to work through the logic of doing the \ operation once all of the T and Y values have been determined. I do not want to run the debugger on that code at the moment in case I interfere with the day-long calculation that has been ongoing.
Walter Roberson
on 22 Dec 2019
My best so far is 0.000659404291380592659 at [1.01384254053041545, 0.000886673535646173398, 1.05390444697181129, 1435253862.25699902]
Your [0.947 0.001 1.027 0.50087e9] has result 0.0711110701550354 so my best so far is roughly 100 times better.
This was not a thorough search; I was just trying to get a feel for the shape of the curve.
mohammad hodaei
on 22 Dec 2019
0 votes
1 Comment
Walter Roberson
on 23 Dec 2019
Are you sure the upper bound for the first parameter is 0.95 ? I get significally better results with a value a little above 1.0 .
mohammad hodaei
on 23 Dec 2019
0 votes
2 Comments
Walter Roberson
on 23 Dec 2019
Under those contraints, at the moment (not a definitive solution!)
Overall best: 0.0657146841025517126 @ [0.949820958237156399, 0.0997551183993498486, 1.23013529228517182, 2716395529.57233524]
Walter Roberson
on 24 Dec 2019
Slightly better:
Overall best: 0.0656687142834942311 @ [0.949830674716950929, 0.0998406073372730546, 1.23014068234019347, 2712675782.83617496]
mohammad hodaei
on 24 Dec 2019
0 votes
4 Comments
Walter Roberson
on 24 Dec 2019
The area near [0.95, 0.001, 1.05, 3.3e9] has a function output of slightly less than 0.1 which is not nearly as good as the 0.0656687142834942311 function output I found within the bounds that you had previously defined.
Walter Roberson
on 24 Dec 2019
Under the constraints you have given, the area you propose has a function value of about 0.083. However, you can do much better (and better than my previous search had found):
Overall best: 0.0520160608888225254 @ [0.99853933634531078, 0.12966369253341048, 1.3585172562555794, 2814165246.48463058]
You have indicated that the first parameter is constrained to be greater than 0 and less than 1. Are there constraints on the other parameters? I do not mean "expected values", I mean "not physically possible for the situation" -- since we must consider the possibility that the methods used by the sources you are looking at were not good enough to find the better solutions.
Walter Roberson
on 24 Dec 2019
The updated best I have found so far is
Overall best: 0.0517619580968272236 @ [0.999999700053125551, 0.125138803966272472, 1.36282292607072097, 2756153431.28588963]
I wonder if the best possible corresponds to the limit of 1 for the first parameter?
Walter Roberson
on 29 Dec 2019
Slightly better:
0.0517549781844199377 @ [0.999999999999999889, 0.125324295792005991, 1.36285494409215868, 2759574809.39368248]
mohammad hodaei
on 1 Jan 2020
0 votes
1 Comment
Walter Roberson
on 1 Jan 2020
As I wrote before, "we must consider the possibility that the methods used by the sources you are looking at were not good enough to find the better solutions."
You can test the residues yourself with the positions I gave, such as [0.999999999999999889, 0.125324295792005991, 1.36285494409215868, 2759574809.39368248]
If you give me a maximum bounds that you would expect as "close enough" to the values you see in the paper, I will run another search and tell you the best value within the region. But do not be surprised if it is no better than 0.08 residue instead of the 0.052-ish that I found further away.
mohammad hodaei
on 2 Jan 2020
Edited: mohammad hodaei
on 2 Jan 2020
0 votes
1 Comment
Walter Roberson
on 2 Jan 2020
Residue is a term used more in fitting, and is often the sum of squared differences between data and the values projected by a model. I do more fitting than optimization, so I tend to slip and refer to the value returned by an objective function as residue even when it is not strictly a fitting process that is being done.
Anyhow, to proceed further you will need to tell me the bounds for the values that you would consider acceptable. You consider 2759574809.39368248 for the fourth parameter to be too far away, so what would acceptable values be (and why should that range be favoured when the value returned by the objective function is much lower near 2759574809.39368248?)
mohammad hodaei
on 2 Jan 2020
0 votes
1 Comment
Walter Roberson
on 2 Jan 2020
Interesting, I have been able to find a better location:
0.000272343781593343897 @ [0.937786744051205545, 0.000959845257910191003, 1.06853745615052587, 3741868634.90991116]
mohammad hodaei
on 2 Jan 2020
0 votes
3 Comments
Walter Roberson
on 3 Jan 2020
Improved version:
0.000265757125070759125 @ [0.92181346247455509, 0.00099584277660562685, 1.06495700859751885, 4014779459.31634855]
At the moment, the 90% confidence intervals are
[0.92181346247455509; 0.937786744657602478]
[0.000959845257286196874; 0.00099584277660562685]
[1.06495700859751885; 1.06853745634979913]
[3741868602.35298824; 4014779459.31634855]
That is quite a wide range for the last parameter, indicating that it does not have a big effect on the output.
I used a custom minimizer I wrote (which is not available for other people to use, because it needs an interface upgrade.)
Walter Roberson
on 3 Jan 2020
Please stop creating a new Answer each time. To respond to something I have said, click on "Comment on this Answer"
Walter Roberson
on 3 Jan 2020
A substantial improvement!
0.000131803755009311688 @ [0.921395426290699593, 0.000999993681106979646, 1.0640667438343101, 4312928343.48244476]
mohammad hodaei
on 3 Jan 2020
0 votes
6 Comments
Walter Roberson
on 3 Jan 2020
Please stop creating a new Answer each time. To respond to something I have said, click on "Comment on this Answer"
Walter Roberson
on 3 Jan 2020
As I indicated before, I used a custom minimizer I wrote (which is not available for other people to use, because it needs an interface upgrade.) The mininizer combines grid searching and Nelder-Mead simplex searching.
mohammad hodaei
on 3 Jan 2020
Walter Roberson
on 4 Jan 2020
fminsearch is Nelder Mead simplex search.
However like all Nelder Mead simplex searches, fminsearch gets stuck in local minima and is not certain to escape them. Also when you are in any particular basin of attraction, fminsearch can end up chasing very small improvements for a long time, under circumstances where what you really need to do is explore a different region.
For these reasons, fminsearch alone is not a good global minimizer for "bumpy" functions; you need to use fminsearch together with some controlling logic to prevent it from wasting time uselessly, while exposing new search areas, but weeding down the search areas to those that appear to be most productive.
Walter Roberson
on 4 Jan 2020
Slight improvement:
0.000131727470727105835 @ [0.92126040045226798, 0.000999968029408149044, 1.06378926780602812, 4315291821.02438068]
90% confidence interval:
[0.92126040045226798; 0.921464207536253022],
[0.00099978973993929284; 0.000999993681106979646],
[1.06378926780602812; 1.06420690461129142],
[4312301563.05776691; 4324361320.68513584]
Walter Roberson
on 4 Jan 2020
Very minor improvement:
0.000131725678063479432 @ [0.921260715970139921, 0.000999968382617359404, 1.06378974249222291, 4315319727.38396549]
90% confidence interval:
[0.921260664174910815; 0.921260757002201669],
[0.000999968319206162972; 0.000999968459532312327],
[1.06378961568252395; 1.06378983531147786],
[4315315536.66939354; 4315321100.97137833]
The difference in function value is on the order of 1e-9, which is not much at all. The difference in the first three coordinates is less than 4E-7. But the difference in the 4th coordinate is about 27906 . That tells us that the solution is barely sensitive to chances in the 4th coordinate.
My graphs suggest that likely it would be possible to get another decimal place out of the locations. The coordinates above should be viewed as "best so far" rather than absolute best. Unfortunately, the eig() step is very expensive to model symbolically, so it is not practical to calculate a derivative in order to prove whether we are at a minima or not.
mohammad hodaei
on 21 Apr 2020
0 votes
mohammad hodaei
on 21 Apr 2020
0 votes
mohammad hodaei
on 21 Apr 2020
0 votes
1 Comment
Walter Roberson
on 21 Apr 2020
S = regexp(fileread('MYCODE.txt'), '\r?\n', 'split');
NS = cellfun(@(s) vectorize(s(~isspace(s))), S, 'uniform', 0);
NS(cellfun(@isempty, NS)) = [];
fid = fopen('REFLECTION.m', 'wt');
fprintf(fid, '%s ...\n', NS{1:end-1});
fprintf(fid, '%s\n', NS{end});
fclose(fid);
Remember to put in an assignment !
mohammad hodaei
on 21 Apr 2020
0 votes
mohammad hodaei
on 21 Apr 2020
0 votes
2 Comments
Walter Roberson
on 22 Apr 2020
1) it adds ',' for separation of each line and Matlab cannot underastand that.
It does not add commas! I tested it, and there is not even one comma in the output!
1) -varphi*(-phi)*
varphi*(((-I)*sqrt(Landa2)*Omega*(1 + phi*(-1 + Zeta2))
2) -varphi.*(-phi).*','varphi.*(((-I).*sqrt(Landa2).*Omega.*(1+phi.*(-1+Zeta2))
No, it produces
-((varphi.*((-phi).* ...
varphi.*(((-I).*sqrt(Landa2).*Omega.*(1+phi.*(-1+Zeta2)).* ...
Notice there are no ' or commas in the output.
Which MATLAB release are you using? And are you sure you copied my code exactly the same as it was?
2) it add extra dot between the variable and it is completey different the priginal code.
It is true that it vectorized the code. When you produce the kind of output you had that does not already have continuations, then it is almost always being produced by some sort of symbolic calculation. Symbolic calculations assume that all variable names are scalars. If this is indeed symbolic generated code and you thought some of your variable names represent matrices and you are expecting algebraic matrix multiplication for the * operator, then you can be fairly sure that the generated code is wrong for that purpose.
If you are expecting that your variable names represent scalars, then vectorizing the code would give you the same result and so vectorization would not hurt.
The time it makes a difference is your variable names do represent scalars, but you later hope to calculate replacing some of the names with vectors or arrays of values and you want "corresponding" elements to be operated on, so like [varphi(1)*-phi(1), varphi(2)*-phi(2)] and so on instead of algebraic matrix multiplication between varphi and phi.
But anyhow, if you have Good Reasons why you believe that the original code really does use algebraic matrix multiplication and that all ^2 operations are intended to represent matrix squaring rather than element-by-element squaring, then you can leave out the vectorization step:
S = regexp(fileread('MYCODE.txt'), '\r?\n', 'split');
S = regexprep(S, '^\s+$', '', 'once');
S(cellfun(@isempty, S)) = [];
fid = fopen('REFLECTION.m', 'wt');
fprintf(fid, '%s ...\n', S{1:end-1});
fprintf(fid, '%s\n', S{end});
fclose(fid);
I suspect that in your code, I refers to sqrt(-1) and E^ is exp(), which is part of the reason that I believe that your code was mechanically generated rather than being hand-written.
If I am correct that your code was symbolically generated code and that all of your operations are really element-by-element operations instead of matrix-multiplication and matrix-power, then I would also suggest that you do
syms c0 E I l Landa1 Landa2 Omega P Q phi R rhof varphi Zeta1 Zeta2
and assign the expression to a variable, and then simplify() the variable. The result is about 30% smaller.
If I am correct about it being element-by-element operations, and that you are planning to use the expression in numeric calculations, I would further suggest that you consider using matlabFunction() and give it the 'File' option to have it write to a file, leaving the default 'optimize' to true. You should also be sure to use the 'vars' option so that you can specify the exact order of variables you want, and have a possibility of bundling variables together into a vector such as you might do if you were running an optimization. Note: using the 'File' option with optimization turned can take a fair time to execute: I only recommend trying this if the function is going to be executed a lot.
Walter Roberson
on 22 Apr 2020
Under the assumption that the expression involves only scalar variables, then an optimized (but not readable) version of it is:
function sout = REFL(c0,E,I,l,Landa1,Landa2,Omega,P,Q,phi,R,rhof,varphi,Zeta1,Zeta2)
%REFL
% SOUT = REFL(C0,E,I,L,LANDA1,LANDA2,OMEGA,P,Q,PHI,R,RHOF,VARPHI,ZETA1,ZETA2)
% This function was generated by the Symbolic Math Toolbox version 8.5.
% 21-Apr-2020 19:55:06
t2 = P.*R;
t3 = Q.*Zeta1;
t4 = Q.*Zeta2;
t5 = R.*Zeta1;
t6 = R.*Zeta2;
t7 = Zeta1.*phi;
t8 = Zeta2.*phi;
t9 = Landa1.^2;
t10 = Landa2.^2;
t11 = Q.^2;
t12 = -Zeta2;
t13 = 1.0./c0;
t14 = -phi;
t15 = phi-1.0;
t16 = 1.0./rhof;
t17 = sqrt(Landa1);
t18 = sqrt(Landa2);
t19 = t17.^3;
t20 = t18.^3;
t21 = P+t3;
t22 = P+t4;
t23 = Q+t5;
t24 = Q+t6;
t25 = -t11;
t26 = Zeta1+t12;
t31 = l.*t17;
t32 = l.*t18;
t33 = -t18;
t39 = t7+t14+1.0;
t40 = t8+t14+1.0;
t48 = t17+t18;
t27 = t21.^2;
t28 = t22.^2;
t29 = t23.^2;
t30 = t24.^2;
t34 = t31.*2.0;
t35 = t32.*2.0;
t36 = t26.^2;
t37 = E.^t31;
t38 = E.^t32;
t41 = -t31;
t42 = -t32;
t45 = t2+t25;
t53 = l.*t48;
t54 = t17+t33;
t43 = E.^t34;
t44 = E.^t35;
t46 = 1.0./t37;
t47 = 1.0./t38;
t49 = -t38;
t50 = t45.^2;
t55 = E.^t53;
t56 = -t53;
t57 = l.*t54;
t63 = Landa1.*Landa2.*t21.*t24.*t37;
t64 = Landa1.*Landa2.*t22.*t23.*t37;
t65 = Landa1.*Landa2.*t21.*t24.*t38;
t66 = Landa1.*Landa2.*t22.*t23.*t38;
t51 = t43-1.0;
t52 = t44-1.0;
t58 = 1.0./t55;
t59 = E.^t57;
t60 = t55-1.0;
t61 = -t57;
t67 = t37+t49;
t68 = Landa1.*Landa2.*t21.*t24.*t46;
t69 = Landa1.*Landa2.*t22.*t23.*t46;
t70 = Landa1.*Landa2.*t21.*t24.*t47;
t71 = Landa1.*Landa2.*t22.*t23.*t47;
t72 = Landa1.*Landa2.*t21.*t24.*t49;
t73 = Landa1.*Landa2.*t22.*t23.*t49;
t62 = 1.0./t59;
t74 = -t68;
t75 = -t69;
t76 = -t70;
t77 = -t71;
t78 = t63+t73;
t79 = t64+t72;
t84 = I.*Landa2.*Omega.*t19.*t21.*t22.*t39.*t67;
t85 = I.*Landa1.*Omega.*t20.*t21.*t22.*t40.*t67;
t86 = I.*Landa2.*Omega.*t19.*t23.*t24.*t39.*t67;
t87 = I.*Landa1.*Omega.*t20.*t23.*t24.*t40.*t67;
t88 = I.*Omega.*t9.*t18.*t27.*t40.*t46.*t51;
t89 = I.*Omega.*t10.*t17.*t28.*t39.*t47.*t52;
t90 = I.*Omega.*t9.*t18.*t29.*t40.*t46.*t51;
t91 = I.*Omega.*t10.*t17.*t30.*t39.*t47.*t52;
t96 = I.*Omega.*t9.*t18.*t21.*t23.*t40.*t46.*t51;
t97 = I.*Omega.*t10.*t17.*t22.*t24.*t39.*t47.*t52;
t98 = I.*Omega.*t9.*t21.*t23.*t33.*t40.*t46.*t51;
t100 = I.*Landa2.*Omega.*t19.*t21.*t22.*t39.*t46.*t60;
t101 = I.*Landa1.*Omega.*t20.*t21.*t22.*t40.*t46.*t60;
t102 = I.*Landa2.*Omega.*t19.*t21.*t22.*t39.*t47.*t60;
t103 = I.*Landa1.*Omega.*t20.*t21.*t22.*t40.*t47.*t60;
t104 = I.*Landa2.*Omega.*t19.*t23.*t24.*t39.*t46.*t60;
t105 = I.*Landa1.*Omega.*t20.*t23.*t24.*t40.*t46.*t60;
t106 = I.*Landa2.*Omega.*t19.*t23.*t24.*t39.*t47.*t60;
t107 = I.*Landa1.*Omega.*t20.*t23.*t24.*t40.*t47.*t60;
t136 = I.*Omega.*t9.*t20.*t21.*t26.*t40.*t45.*t51.*t58;
t137 = I.*Omega.*t10.*t19.*t22.*t26.*t39.*t45.*t52.*t58;
t138 = I.*Omega.*t9.*t20.*t23.*t26.*t40.*t45.*t51.*t58;
t139 = I.*Omega.*t10.*t19.*t24.*t26.*t39.*t45.*t52.*t58;
t140 = I.*Omega.*t10.*t19.*t22.*t26.*t39.*t45.*t52.*t59;
t141 = I.*Omega.*t10.*t19.*t24.*t26.*t39.*t45.*t52.*t59;
t148 = t9.*t10.*t13.*t16.*t36.*t50.*t51.*t52.*t58.*varphi;
t80 = t63+t77;
t81 = t64+t76;
t82 = t65+t75;
t83 = t66+t74;
t92 = t68+t77;
t93 = t69+t76;
t94 = -t89;
t95 = -t91;
t99 = -t97;
t108 = -t100;
t109 = -t102;
t110 = -t104;
t111 = -t106;
t112 = I.*Omega.*t17.*t39.*t78;
t113 = I.*Omega.*t17.*t39.*t79;
t114 = I.*Omega.*t18.*t40.*t78;
t115 = I.*Omega.*t18.*t40.*t79;
t124 = t58.*t84;
t125 = t58.*t85;
t126 = t58.*t86;
t127 = t58.*t87;
t142 = -t136;
t143 = -t138;
t144 = I.*Omega.*t9.*t20.*t21.*t26.*t40.*t45.*t51.*t62;
t145 = I.*Omega.*t9.*t20.*t23.*t26.*t40.*t45.*t51.*t62;
t116 = I.*Omega.*t17.*t39.*t80;
t117 = I.*Omega.*t17.*t39.*t81;
t118 = I.*Omega.*t17.*t39.*t82;
t119 = I.*Omega.*t17.*t39.*t83;
t120 = -I.*Omega.*t33.*t40.*t80;
t121 = -I.*Omega.*t33.*t40.*t81;
t122 = -I.*Omega.*t33.*t40.*t82;
t123 = -I.*Omega.*t33.*t40.*t83;
t128 = I.*Omega.*t17.*t39.*t92;
t129 = I.*Omega.*t17.*t39.*t93;
t132 = I.*Omega.*t18.*t40.*t92;
t133 = I.*Omega.*t18.*t40.*t93;
t134 = -I.*Omega.*t18.*t40.*t82;
t135 = -I.*Omega.*t18.*t40.*t83;
t146 = -t144;
t147 = -t145;
t149 = t84+t88+t108;
t150 = t85+t94+t103;
t151 = t86+t90+t110;
t152 = t87+t95+t107;
t153 = t90+t111+t126;
t154 = t95+t105+t127;
t155 = t88+t109+t124;
t156 = t94+t101+t125;
t179 = -I.*Omega.*t17.*t39.*t46.*(t97-I.*Omega.*t18.*t40.*t80+I.*Omega.*t33.*t40.*t78);
t180 = -I.*Omega.*t17.*t39.*t46.*(t97-I.*Omega.*t18.*t40.*t81+I.*Omega.*t33.*t40.*t79);
t185 = I.*Omega.*t17.*t39.*t46.*(t97-I.*Omega.*t18.*t40.*t80+I.*Omega.*t33.*t40.*t78);
t186 = I.*Omega.*t17.*t39.*t46.*(t97-I.*Omega.*t18.*t40.*t81+I.*Omega.*t33.*t40.*t79);
t191 = t14.*varphi.*(t136-t137-t140+t144);
t192 = -t15.*varphi.*(t138-t139-t141+t145);
t193 = -t13.*t16.*varphi.*(t136-t137-t140+t144);
t194 = -t13.*t16.*varphi.*(t138-t139-t141+t145);
t195 = t13.*t16.*varphi.*(t136-t137-t140+t144);
t196 = t13.*t16.*varphi.*(t138-t139-t141+t145);
t198 = -t13.*t16.*varphi.*(-t148+phi.*varphi.*(t136-t137-t140+t144)+t15.*varphi.*(t138-t139-t141+t145));
t199 = t13.*t16.*varphi.*(-t148+phi.*varphi.*(t136-t137-t140+t144)+t15.*varphi.*(t138-t139-t141+t145));
t130 = -t118;
t131 = -t119;
t159 = t99+t114+t120;
t160 = t99+t115+t121;
t161 = I.*Omega.*t18.*t40.*t47.*t149;
t162 = I.*Omega.*t17.*t39.*t46.*t150;
t163 = I.*Omega.*t18.*t40.*t47.*t151;
t164 = I.*Omega.*t17.*t39.*t46.*t152;
t167 = t98+t116+t128;
t168 = t98+t117+t129;
t169 = t97+t133+t134;
t170 = t97+t132+t135;
t171 = I.*Omega.*t17.*t37.*t39.*t156;
t172 = I.*Omega.*t18.*t38.*t40.*t155;
t173 = I.*Omega.*t17.*t37.*t39.*t154;
t174 = I.*Omega.*t18.*t38.*t40.*t153;
t175 = I.*Omega.*t33.*t38.*t40.*t155;
t176 = I.*Omega.*t33.*t38.*t40.*t153;
t181 = I.*Omega.*t33.*t40.*t49.*(-t96+t116+t128);
t182 = I.*Omega.*t33.*t40.*t49.*(-t96+t117+t129);
t189 = t137+t140+t142+t146;
t190 = t139+t141+t143+t147;
t197 = t148+t191+t192;
t157 = t96+t112+t131;
t158 = t96+t113+t130;
t165 = -t162;
t166 = -t164;
t183 = I.*Omega.*t17.*t37.*t39.*t169;
t184 = I.*Omega.*t17.*t37.*t39.*t170;
t177 = I.*Omega.*t18.*t40.*t47.*t157;
t178 = I.*Omega.*t18.*t40.*t47.*t158;
t187 = -t183;
t188 = -t184;
t200 = t161+t165+t171+t175;
t201 = t163+t166+t173+t176;
t202 = phi.*t200.*varphi;
t203 = t15.*t201.*varphi;
t204 = t177+t181+t185+t188;
t205 = t178+t182+t186+t187;
t206 = phi.*t205.*varphi;
t207 = t15.*t204.*varphi;
t208 = t195+t202+t207;
t209 = t196+t203+t206;
t210 = phi.*t208.*varphi;
t211 = t15.*t209.*varphi;
t212 = t199+t210+t211;
t213 = 1.0./t212;
sout = t199.*t213-t15.*t213.*varphi.*(t196+t203+phi.*t204.*varphi)+t14.*t213.*varphi.*(t195+t202+t15.*t205.*varphi);
mohammad hodaei
on 22 Apr 2020
0 votes
1 Comment
Walter Roberson
on 22 Apr 2020
NS is a variable used for calculating the output, and is not the output itself. The output is written to REFLECTION.m
>> type REFLECTION.m
Landa1=(1/2)*(a*(I*Omega)^(3/2)*(Pp + 2*Qq + Rr) + ...
Omega^2*(2*Qq*rho12 - Pp*rho22 - rho11*Rr) + ...
sqrt((-I)* ...
Omega^3*(a^2*(Pp + 2*Qq + Rr)^2 + ...
I*Omega*(4*Qq^2*rho11*rho22 + Pp^2*rho22^2 + ...
4*Pp*rho12^2*Rr - 2*Pp*rho11*rho22*Rr + ...
rho11^2*Rr^2 - 4*Qq*rho12*(Pp*rho22 + rho11*Rr)) + ...
2*a*sqrt(I*Omega)* ...
(Pp^2*rho22 + 2*Qq^2*(rho11 + rho22) - ...
Pp*(rho11 + 4*rho12 + rho22)*Rr + ...
rho11*Rr^2 + ...
2*Qq*((-Pp)*rho12 + Pp*rho22 + rho11*Rr - rho12*Rr)))));
mohammad hodaei
on 22 Apr 2020
0 votes
3 Comments
mohammad hodaei
on 23 Apr 2020
mohammad hodaei
on 23 Apr 2020
Walter Roberson
on 23 Apr 2020
The version I posted at https://www.mathworks.com/matlabcentral/answers/497310-your-objective-function-must-return-a-scalar-value#comment_832540 does not vectorize.
Repeating that:
S = regexp(fileread('MYCODE.txt'), '\r?\n', 'split');
S = regexprep(S, '^\s+$', '', 'once');
S(cellfun(@isempty, S)) = [];
fid = fopen('REFLECTION.m', 'wt');
fprintf(fid, '%s ...\n', S{1:end-1});
fprintf(fid, '%s\n', S{end});
fclose(fid);
What this does:
- Reads in the file MYCODE.txt as a single character vector
- Divides that character vector up into lines by looking for carriage return and linefeed, giving a cell array of character vectors
- looks for lines that contain only whitespace, and changes them to be empty lines. The initial part of your long MYCODE.txt contained some lines that had nothing in them except some spaces
- looks for empty lines (either because they were originally empty or because they had nothing but blanks in them and were changed to empty) and deletes those
- opens a file REFLECTION.m for output
- write out all lines except the last, adding <space><dot><dot><dot> at the end of each line
- writes out the last line without putting on the continuation mark
- closes the file.
mohammad hodaei
on 30 Apr 2020
Edited: Walter Roberson
on 30 Apr 2020
1 Comment
Walter Roberson
on 30 Apr 2020
fig = openfig('example.fig');
dataObjs = findobj(fig, '-property', 'XData');
ndata = length(dataObjs);
[x{1:ndata}] = dataObjs.XData;
[y{1:ndata}] = dataObjs.YData;
One of the two figures has a single child that is an axes, and that axes has one child that is a line object.
The other of the two figures has two children, one of which is a legend and the other is an axes; the axes has two line objects as its children.
Your code was assuming that fig only ever has one child, or else that the first child will happen to be the one you are interested in. Because of the way that graphics objects are ordered, it is far more likely that you would be interested in the last child rather than the first.
Remember that
variable = non_scalar.property
is equivalent to
variable = non_scalar(1).property, non_scalar(2).property, non_scalar(3).property and so on
because of structure expansion. Your right hand side effectively becomes a comma-separated list. But you only named a single location on your left hand side, so you were getting the result only from the first one, axObjs(1).Children .
mohammad hodaei
on 30 Apr 2020
0 votes
mohammad hodaei
on 2 May 2020
0 votes
1 Comment
Walter Roberson
on 2 May 2020
You cannot just rename a .pdf file to be .fig .
You would need to load the contents of the .pdf file into MATLAB (somehow), display what you could, and save as .fig .
Or you could use a program such as ghostscript to convert the pdf into an image, and then read the image in MATLAB and display it in a figure. https://stackoverflow.com/questions/653380/converting-a-pdf-to-png
mohammad hodaei
on 13 May 2020
0 votes
6 Comments
Walter Roberson
on 13 May 2020
Please open a new Question for this. It has nothing to do with objective functions having to return scalar values.
mohammad hodaei
on 13 May 2020
Walter Roberson
on 14 May 2020
Look at the title of this question. It is
Your objective function must return a scalar value.
However, your difficulty is about
Index in position 1 is invalid. Array indices must be positive integers or logical values.
This Question is the wrong place to post your query. You should use
and ask your question there.
mohammad hodaei
on 14 May 2020
mohammad hodaei
on 16 May 2020
Walter Roberson
on 19 May 2020
mohammad hodaei
on 13 May 2020
0 votes
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!