Your objective function must return a scalar value.

10 views (last 30 days)
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

mohammad hodaei
mohammad hodaei on 30 Apr 2020
Thanks for your response but I got
[x{1:ndata}] = dataObjs.XData ;
Unrecognized method, property, or field 'XData' for class
'matlab.graphics.GraphicsPlaceholder'.
  2 Comments
Walter Roberson
Walter Roberson on 14 May 2020
This is not an Answer to your original question about objective function returning scalar value; it is a comment about a passing difficulty two topics later. :(

Sign in to comment.

More Answers (26)

Walter Roberson
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
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

Sign in to comment.


mohammad hodaei
mohammad hodaei on 19 Dec 2019
Dear Walter,
Thank you for your time and help.
I went through what you have written and explained. Using GA-GENETIC ALGORITHM, Isee "Not enough input arguments.".
I changed it to "fmincon-Constrained nonlinear minimization" and I checked it. it is wrking well. The reason I am saying because I ran my code with the defined value and then I asked the optimization to give me that value. In fact, this is inverse problem technique to find disease inside the body. Finally I got the following valoues after optimization respect to the first ones that I gave them.
initial: x= 0.83, 0.35, 0.95
after onptimization: 0.81, 0.35, 0.917
I do apprecite for you great help.
Walter Roberson answered perfectly completely
  2 Comments
Walter Roberson
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
mohammad hodaei on 19 Dec 2019
You are right. I see it does not work for more than one parametrs. As I explained in my previous commnets, it is working well when I have one unknown parametrs.
Do you have any suggestion to fix it? I need to find 3-5 parametres. This problem is regarding Osteoporosis prediction.
Thanks
M.H.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 19 Dec 2019
Dear Walter,
Your help, time, and effort regarding my research has really appreciation. Thanks a lot.
I used Z = double(vpa(T,16)\Y). The result did not change. I prefer to know your idea in advacne before changing anything else. It seems I am making a mistake in changing or whatever else.
I also attached my files again, if you like, to have a look at what I changed.
Am I right? Is that what you mean?
Sincerely
M.H.
  1 Comment
Walter Roberson
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.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 19 Dec 2019
Dear Walter,
I read your another comments regarding other x's. I have to look for a few parameters. for one parameters, my code works well. In fact, your suggestion regarding sum was so helpful. But as soon as I define x in function and appply x(1), x(2), x(3) as unknown parametres, I got results which are discrepancy.
Any suggestion please!
Best
M.H.
  1 Comment
Walter Roberson
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.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 19 Dec 2019
You are right. But We need to use this code as , inverse problem technique, to find some information about what we do not have any sense about inside it. Then we will be able to say our patience has Osteoporosis or not. It is also possible to find brain tumor or brist cance.
Good news to tell you I am using simulannealbnd-Simulated annealing algorithm as it return back three parametrs loke below
initial: 0.95 1.05 0.001
by this method: 0.919 1.001 0.001
I need to see I can find the better way to find the exactly?
Any suggestion would be apprecited.
Thanks
M.H.

mohammad hodaei
mohammad hodaei on 20 Dec 2019
Dear Walter,
I defined x(1)...x(4) for four parameters. Until three parameters, i got resonable solution, but by adding the forth ones, It is little far.
i GOT THESE
Initial: 0.95 0.001 1.05 3.3e9
optimization: 0.947 0.001 1.027 0.50087e9
As you see , the last ones, 0.50087e9, is not matched with the intial ones.
Please run it based on the attached file(fig) in optimization.
Note that it little takes time.
Thanks
M.H.
  2 Comments
Walter Roberson
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
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.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 22 Dec 2019
Greetings Walter,
Attatched, please find my new results. As you can see, the last ones should be 3.3e9 while it is 4.646e9.
This is the only problem I see in my result, if not, others are almost fine.
note that I times e9 in the code, so you can see 4.646.
Any advice
Thanks
M.H.
  1 Comment
Walter Roberson
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 .

Sign in to comment.


mohammad hodaei
mohammad hodaei on 23 Dec 2019
Dear Walter,
Yes, the uppper bound cannot be 1 or more than 1. it is maximum 0.9999...
Also, it cannot be zero.
the main problem or the only problem is the last parametre. it is 3.3e9. I got it bertween 4 and 5e9.
Others are almost acdeptable because their errors are between 2-6 percent.
Thanks
M.H.
  2 Comments
Walter Roberson
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
Walter Roberson on 24 Dec 2019
Slightly better:
Overall best: 0.0656687142834942311 @ [0.949830674716950929, 0.0998406073372730546, 1.23014068234019347, 2712675782.83617496]

Sign in to comment.


mohammad hodaei
mohammad hodaei on 24 Dec 2019
Dear Walter,
Could you please let me know what is it?
Overall best: 0.0656687142834942311 @ [0.949830674716950929, 0.0998406073372730546, 1.23014068234019347, 2712675782.83617496]
0.949830674716950929 is berfect. 0.0998406073372730546 is far from 0.001. 1.23014068234019347 is far from 1.05.
2712675782.83617496 is far from 3.2 e9.
I need to reach 0.95, 0.001, 1.05, 3.3e9.
Please advise
M.H.
  4 Comments
Walter Roberson
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
Walter Roberson on 29 Dec 2019
Slightly better:
0.0517549781844199377 @ [0.999999999999999889, 0.125324295792005991, 1.36285494409215868, 2759574809.39368248]

Sign in to comment.


mohammad hodaei
mohammad hodaei on 1 Jan 2020
Dear Walter,
By three parameters, it works well, but any more pparamters, I have provblem.
parametres: 0.95 0.001 1.05
results: 0.9283987305686014 8.367384993495985E-4 1.047511082358547
see how close together.
Thanks
M.H.
  1 Comment
Walter Roberson
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.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 2 Jan 2020
Edited: mohammad hodaei on 2 Jan 2020
Thanks for your email. Sure, i can wait to see your new results!
But, sorry , waht do you mean residue?
Thanks
M.H.
  1 Comment
Walter Roberson
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?)

Sign in to comment.


mohammad hodaei
mohammad hodaei on 2 Jan 2020
Thanks again!
I am using three parametres and the results are almost acceptable.
Results: 0.928 0.001 1.048
real parameters: 0.95 0.001 1.05
As soon as I increase my parametres to more than three, I get terrible results.
the bounds could be
for three parameters
[0.1 0.0001 1] [0.95 0.001 2]
for four parameters
[0.1 0.0001 1 1e9] [0.95 0.001 2 5e9]
real parameters: 0.95 0.001 1.05 3.3e9
Thanks
M.H.
  1 Comment
Walter Roberson
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]

Sign in to comment.


mohammad hodaei
mohammad hodaei on 2 Jan 2020
Dear Walter,
your results including 4 parameters are fantastic.
Please let me know which method you used to get 4 parameters?
real parameters 0.95 0.001 1.05 3.3e9
Yours: 0.94 0.00096 1.068 3.74e9
How did you get these?
Thanks
M.H.
  3 Comments
Walter Roberson
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
Walter Roberson on 3 Jan 2020
A substantial improvement!
0.000131803755009311688 @ [0.921395426290699593, 0.000999993681106979646, 1.0640667438343101, 4312928343.48244476]

Sign in to comment.


mohammad hodaei
mohammad hodaei on 3 Jan 2020
Dear Walter,
Could you please let me know which method you are using?
I see you are getting the results for more than three parametrs. please let me know how?
Best
M.H.
  6 Comments
Walter Roberson
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
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.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 21 Apr 2020
Hi Walter, Hope you are doing well.
I had a good experience with you regarding my matlab question in inverse problem.
I have another question and I dom appreciate answering me.
I have 277 lines Matlab program. As you know I need to use ... to be able to be run with Matlab.
do you have any suggestions?
It is hard to use ... after each symbol (*-+/), so I am looking for another easy solution.
If even you can show me how we can write them in just one line, then I can run it.
Any suggestion please?
mohodaei@gmail.com
Best
M.H.

mohammad hodaei
mohammad hodaei on 21 Apr 2020
SEE THE ATTACHED FILE PLEASE

mohammad hodaei
mohammad hodaei on 21 Apr 2020
Thanks for answering my question.
Attached, please find my mfile. I also put my code in text too.
as you see my code is 2777 lines. so Matlab cannot read them as long as they are written like below.
-((varphi*((-phi)*
varphi*(((-I)*sqrt(Landa2)*Omega*(1 + phi*(-1 + Zeta2))*
so I have to use ... to be readabble nby Matlab like below:
-((varphi*((-phi)*........
varphi*(((-I)*sqrt(Landa2)*Omega*(1 + phi*(-1 + Zeta2))*........
my question: this code is 2777 lines and it is hard for me to put all lines .......
do you have any suggestions to help me?
A copy of my code is attached as well in both format text and mfile.
Best
M.H.
  1 Comment
Walter Roberson
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 !

Sign in to comment.


mohammad hodaei
mohammad hodaei on 21 Apr 2020
Sure but i have this error.
Error in CodeLiner (line 1)
S = regexp(fileread('MYCODE.txt'), '\r?\n', 'split ');
Thanks and advice
M.H.

mohammad hodaei
mohammad hodaei on 21 Apr 2020
Sorry, but it was not applicable due to the following reaons:
1) it adds ',' for separation of each line and Matlab cannot underastand that.
2) it add extra dot between the variable and it is completey different the priginal code.
for example see 1 and 2. 1 is original part of code and 2 is based on your code.
1) -varphi*(-phi)*
varphi*(((-I)*sqrt(Landa2)*Omega*(1 + phi*(-1 + Zeta2))
2) -varphi.*(-phi).*','varphi.*(((-I).*sqrt(Landa2).*Omega.*(1+phi.*(-1+Zeta2))
Thanks and advice
M.H.
  2 Comments
Walter Roberson
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
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);

Sign in to comment.


mohammad hodaei
mohammad hodaei on 22 Apr 2020
I did not get that. Sorry!
After using your code,below, I got the attached file for a simple code.
the simple code is attached too.
Could you take time and let me know the mfile you get by that text or that one I found?
may be you are right. I have problem in output. but please tghanks again!
Sincerely
M.H.
S = regexp(fileread('MYCODE.txt'),'\r?\n','split');
NS = cellfun(@(s) vectorize(s(~isspace(s))), S, 'uniform', 0 );
NS(cellfun(@isempty, NS )) = [];
  1 Comment
Walter Roberson
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)))));

Sign in to comment.


mohammad hodaei
mohammad hodaei on 22 Apr 2020
I did, but it gave me this
""Error using fopen
The file mode for fopen can not contain modes other than r, w, a, +, b, A, W or t.""
Any suggestion please
Best
M.H.
  3 Comments
mohammad hodaei
mohammad hodaei on 23 Apr 2020
Dear Walter, Finally
I got the folllowing result. But yours does not haver dots as many as I have.
Any advice please??????
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)))));
Walter Roberson
Walter Roberson on 23 Apr 2020
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:
  1. Reads in the file MYCODE.txt as a single character vector
  2. Divides that character vector up into lines by looking for carriage return and linefeed, giving a cell array of character vectors
  3. 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
  4. 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
  5. opens a file REFLECTION.m for output
  6. write out all lines except the last, adding <space><dot><dot><dot> at the end of each line
  7. writes out the last line without putting on the continuation mark
  8. closes the file.

Sign in to comment.


mohammad hodaei
mohammad hodaei on 30 Apr 2020
Edited: Walter Roberson on 30 Apr 2020
Dear Walter,
Attached, you can see two figures.
I used
fig = openfig('example.fig');
axObjs = fig.Children
dataObjs = axObjs.Children
x = dataObjs(1).XData
in one of them,SigmoisFunction.fig, I get data but in another one, ReflectionValidation, i get 0x0 empty GraphicsPlaceholder array
I put HandleVisibility on but it does not work.
any siggestion please?
  1 Comment
Walter Roberson
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 .

Sign in to comment.


mohammad hodaei
mohammad hodaei on 30 Apr 2020
Dear Walter,
Attached, you can see two figures.
I used
fig = openfig('example.fig');
axObjs = fig.Children
dataObjs = axObjs.Children
x = dataObjs(1).XData
in one of them,SigmoisFunction.fig, I get data but in another one, ReflectionValidation, i get 0x0 empty GraphicsPlaceholder array
I put HandleVisibility on but it does not work.
any siggestion please?

mohammad hodaei
mohammad hodaei on 2 May 2020
It worked. Thanks.
Another request please.
do you have any idea how I can chnege the pdf figure to fig.file?
I converted my pdf to fig, then I wanted to opened it it says:
see attached file please
Any idea how I get a data from a figure.
  1 Comment
Walter Roberson
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

Sign in to comment.


mohammad hodaei
mohammad hodaei on 13 May 2020
Dear Walter,
First, Thank you for taking time and answering all of my questions during my Ph.D.
I have a function in s-domain, TR. I used Durbin to bring it in time domain.
the function has a few parameters. I decidede to find one of the parameters using inverse problem.
I have a file of another function which I need to match my function with it, then I can find my parameter.(MINIMIZATION)
My concern is to know how I use inverse problem here? after Durbin or...
I decided to add a few lines at the end of my durbin regarding inverse problem(minimization) and start my durbin code withy a function name [function y=f(x)].
attached, you can see my TR, f (include Durbin+ a few more line for inverse) and a mat file including the data of another function which I need to have minimization with it. Also, there is a mfile with name fint as it is just a function.
when I use optimization, I get "Index in position 1 is invalid. Array indices must be positive integers or logical values."
First: is my procedure right? if so, what is your idea please?
please find my files and see inside f.m
Again, thank you for your time and answers.
Best
M.H.
  6 Comments

Sign in to comment.


mohammad hodaei
mohammad hodaei on 13 May 2020
Dear Walter,
I tried to contact you from different ways to be sure about your request.
I also put my question here. is that what you asked me to do?
sorry for any inconvenience
Please see thettached file based on my previous question.
Should I provide more infotmaion. please just let me know.
Best
M.H.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!