How to eliminate or exclude complix numbers from an array solution?

292 views (last 30 days)
Hello everyone,
Is there any way to eliminate the complex numbers (real and imaginary parts) from a array solution. Or can I exclude the complex elements so the solution will just include the real numbers. For example, in the next array I just want the real elements ( the first four elements. Thanks for any suggestion.
sol =
2.7081096834177947291918098879734
2.4539692532957056897462100829847
1.1392825187840759291489282043638
-4.0494620185638170836265057842329
0.88295725432536163128309307530013 - 0.1308999971886537594046901453934*i
0.1308999971886537594046901453934*i + 0.88295725432536163128309307530013
0.1528510148755374525528985048959*i - 2.0089069727922412635133142708447
- 0.1528510148755374525528985048959*i - 2.0089069727922412635133142708447
  2 Comments
srijesh raghavan
srijesh raghavan on 21 Apr 2020
%please help me find the values of l and m : where func should be maximum
syms l m;
w1= 981*l^(2)*(2-l)^(2)+(490.5*l*(2-l-m)*(-2*l^(2)-(m^(2))+(4*l)+(4*m)-(2*l*m))/2);
%
%
%
%
w2= (-981*l*(2-l-m)*(2*l^(2)+m^(2)+(2*l*m)-(4*l)-(4*m)/2)+(490.5*(l+m)*(2-l-m)*(-2*l^(2)-2*m^(2)+(4*l)+(4*m)-(4*l*m))/2));
func = (2*w1+w2)/(2*w1^(2)+w2^2);
eqn1 = diff(func,l)
eqn2 = diff(func,m)
% sol= solve(eqn1,eqn2,'l')
sol = solve(eqn1,eqn2)
lvalue = double(sol.l);
mvalue = double(sol.m);
Walter Roberson
Walter Roberson on 8 Sep 2020
fs = double(subs(func, {l,m}, {lvalue, mvalue}));
mask = imag(fs) == 0;
[maxvalue, maxidx] = max(fs(mask));
selected_l = lvalue(mask);
selected_m = mvalue(mask);
best_l = selected_l(maxidx);
best_m = selected_m(maxidx);

Sign in to comment.

Accepted Answer

Matt J
Matt J on 14 Feb 2016
sol = sol(sol==real(sol));

More Answers (1)

Jose Perez-Sanabria
Jose Perez-Sanabria on 7 Sep 2020
x=x(imag(y)==0);
y=y(imag(y)==0);
  2 Comments
Ataklti Kahsay
Ataklti Kahsay on 17 Jun 2021
Dear sirs how can i solve this error for my multiobjective problem?
Obj =
1.0e+13 *
2.3579 + 0.0097i 0.0106 + 0.0000i
Obj =
1.0e+11 *
4.0416 + 2.4312i 0.0186 + 0.0110i
Obj =
1.0e+12 *
1.8716 + 0.2003i 0.0085 + 0.0009i
Obj =
1.0e+11 *
8.6100 + 3.2635i 0.0392 + 0.0147i
Obj =
1.0e+11 *
3.5387 + 3.1425i 0.0164 + 0.0142i
Obj =
1.0e+12 *
1.2556 + 0.2470i 0.0057 + 0.0011i
Obj =
1.0e+11 *
3.1311 + 2.6271i 0.0145 + 0.0118i
Obj =
1.0e+11 *
3.3460 + 2.5507i 0.0155 + 0.0115i
Obj =
1.0e+11 *
4.9544 + 1.6274i 0.0227 + 0.0073i
Obj =
1.0e+12 *
1.8264 + 0.1878i 0.0083 + 0.0008i
Obj =
1.0e+11 *
4.3402 + 2.2907i 0.0199 + 0.0103i
Obj =
1.0e+11 *
5.5973 + 1.6902i 0.0256 + 0.0076i
Obj =
1.0e+11 *
2.7293 + 3.0808i 0.0127 + 0.0139i
Obj =
1.0e+11 *
5.0123 + 1.5909i 0.0229 + 0.0072i
Obj =
1.0e+11 *
4.0736 + 1.6777i 0.0187 + 0.0076i
Obj =
1.0e+11 *
8.2966 + 1.5221i 0.0377 + 0.0069i
Obj =
1.0e+11 *
6.3667 + 1.6824i 0.0290 + 0.0076i
Obj =
1.0e+11 *
3.6570 + 2.2086i 0.0169 + 0.0100i
Obj =
1.0e+12 *
2.1831 + 0.1903i 0.0099 + 0.0009i
Obj =
1.0e+11 *
5.5108 + 3.2337i 0.0253 + 0.0146i
Obj =
1.0e+12 *
1.1843 + 0.2196i 0.0054 + 0.0010i
Obj =
1.0e+11 *
7.1247 + 1.5626i 0.0325 + 0.0070i
Obj =
1.0e+11 *
3.0163 + 3.1983i 0.0140 + 0.0144i
Obj =
1.0e+11 *
2.9912 + 3.3404i 0.0139 + 0.0151i
Obj =
1.0e+12 *
1.2569 + 0.2216i 0.0057 + 0.0010i
Obj =
1.0e+12 *
1.0743 + 0.1343i 0.0049 + 0.0006i
Obj =
1.0e+11 *
6.3668 + 1.7646i 0.0291 + 0.0080i
Obj =
1.0e+11 *
6.3229 + 2.1445i 0.0289 + 0.0097i
Obj =
1.0e+11 *
4.0615 + 2.5677i 0.0187 + 0.0116i
Obj =
1.0e+11 *
5.0402 + 1.7757i 0.0231 + 0.0080i
Obj =
1.0e+11 *
7.4331 + 2.5849i 0.0339 + 0.0116i
Obj =
1.0e+11 *
4.5437 + 2.8134i 0.0209 + 0.0127i
Obj =
1.0e+12 *
7.7264 + 0.1666i 0.0348 + 0.0008i
Obj =
1.0e+11 *
3.9587 + 1.3630i 0.0182 + 0.0061i
Obj =
1.0e+12 *
1.3352 + 0.1866i 0.0061 + 0.0008i
Obj =
1.0e+12 *
1.3655 + 0.1923i 0.0062 + 0.0009i
Obj =
1.0e+11 *
6.8164 + 2.1813i 0.0311 + 0.0098i
Obj =
1.0e+11 *
6.9803 + 2.3751i 0.0318 + 0.0107i
Obj =
1.0e+11 *
8.7241 + 1.2626i 0.0397 + 0.0057i
Obj =
1.0e+11 *
9.4473 + 1.7764i 0.0429 + 0.0080i
Obj =
1.0e+11 *
2.9838 + 2.9953i 0.0139 + 0.0135i
Obj =
1.0e+11 *
4.7738 + 1.0734i 0.0218 + 0.0048i
Obj =
1.0e+11 *
9.5048 + 3.2442i 0.0433 + 0.0146i
Obj =
1.0e+11 *
3.4871 + 2.8526i 0.0161 + 0.0129i
Obj =
1.0e+11 *
7.8562 + 2.3313i 0.0358 + 0.0105i
Obj =
1.0e+11 *
4.8092 + 2.5321i 0.0221 + 0.0114i
Obj =
1.0e+11 *
5.7507 + 1.7087i 0.0263 + 0.0077i
Obj =
1.0e+11 *
3.3361 + 2.2627i 0.0154 + 0.0102i
Obj =
1.0e+11 *
4.4049 + 3.3091i 0.0203 + 0.0149i
Obj =
1.0e+11 *
3.0744 + 2.4623i 0.0142 + 0.0111i
Warning: One or more feasible individuals has a complex fitness function value. gamultiobj is using the real part of the
fitness function values
> In rankAndDistance (line 21)
In gamultiobjMakeState (line 199)
In gamultiobjsolve (line 20)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
> In gaplotpareto>plotFirstFront (line 72)
In gaplotpareto (line 28)
In gadsplot>callOnePlotFcn (line 194)
In gadsplot (line 141)
In gamultiobjsolve (line 24)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Warning: Using only the real component of complex data.
> In matlab.graphics.chart.internal.getRealData (line 52)
In bar (line 139)
In gaplotscores (line 41)
In gadsplot>callOnePlotFcn (line 194)
In gadsplot (line 141)
In gamultiobjsolve (line 24)
In gamultiobj (line 304)
In Main_Genetic_Algorithm (line 41)
Obj =
1.0e+12 *
1.1881 + 0.2512i 0.0054 + 0.0011i
Obj =
1.0e+11 *
3.1144 + 3.0470i 0.0145 + 0.0137i
Obj =
1.0e+11 *
3.7438 + 2.8514i 0.0173 + 0.0128i
Obj =
1.0e+11 *
3.0983 + 3.0006i 0.0144 + 0.0135i
Obj =
1.0e+11 *
4.5208 + 1.6571i 0.0207 + 0.0075i
Obj =
1.0e+11 *
3.0157 + 2.9600i 0.0140 + 0.0133i
Obj =
1.0e+11 *
4.2461 + 1.4659i 0.0195 + 0.0066i
Obj =
1.0e+11 *
7.9336 + 2.3053i 0.0361 + 0.0104i
Obj =
1.0e+11 *
6.1800 + 2.1070i 0.0282 + 0.0095i
Obj =
1.0e+11 *
2.9913 + 3.3399i 0.0139 + 0.0150i
Obj =
1.0e+11 *
4.2126 + 1.9457i 0.0194 + 0.0088i
Obj =
1.0e+11 *
3.7206 + 2.2488i 0.0171 + 0.0101i
Obj =
1.0e+11 *
3.9526 + 1.8088i 0.0182 + 0.0081i
Obj =
1.0e+11 *
6.5851 + 1.6861i 0.0300 + 0.0076i
Obj =
1.0e+11 *
5.0697 + 1.6949i 0.0232 + 0.0076i
Obj =
1.0e+11 *
9.2479 + 1.5526i 0.0420 + 0.0070i
Obj =
1.0e+11 *
3.1204 + 3.0248i 0.0145 + 0.0136i
Obj =
1.0e+11 *
3.6741 + 2.1323i 0.0169 + 0.0096i
Obj =
1.0e+11 *
3.9378 + 2.4991i 0.0181 + 0.0113i
Obj =
1.0e+11 *
4.5609 + 1.5928i 0.0209 + 0.0072i
Obj =
1.0e+11 *
3.8672 + 1.5589i 0.0178 + 0.0070i
Obj =
1.0e+11 *
3.2057 + 2.6197i 0.0149 + 0.0118i
Obj =
1.0e+11 *
3.8968 + 1.9892i 0.0179 + 0.0090i
Obj =
1.0e+11 *
5.5084 + 2.1275i 0.0252 + 0.0096i
Obj =
1.0e+11 *
3.2324 + 2.5177i 0.0150 + 0.0113i
Obj =
1.0e+11 *
4.1460 + 2.3829i 0.0191 + 0.0107i
Obj =
1.0e+11 *
3.1253 + 2.9068i 0.0145 + 0.0131i
Obj =
1.0e+11 *
3.0991 + 3.2528i 0.0144 + 0.0147i
Obj =
1.0e+11 *
6.4726 + 2.5683i 0.0296 + 0.0116i
Obj =
1.0e+11 *
4.1689 + 2.1560i 0.0192 + 0.0097i
Obj =
1.0e+11 *
8.2382 + 1.2482i 0.0375 + 0.0056i
Obj =
1.0e+11 *
3.2638 + 2.9144i 0.0151 + 0.0131i
Obj =
1.0e+11 *
3.2686 + 3.0957i 0.0152 + 0.0139i
Obj =
1.0e+11 *
3.7187 + 2.2086i 0.0171 + 0.0100i
Obj =
1.0e+11 *
3.9584 + 1.3633i 0.0182 + 0.0061i
Obj =
1.0e+11 *
3.9483 + 1.3630i 0.0181 + 0.0061i
Obj =
1.0e+11 *
3.5312 + 3.1425i 0.0163 + 0.0142i
Obj =
1.0e+12 *
1.2013 + 0.2470i 0.0055 + 0.0011i
Obj =
1.0e+11 *
3.3136 + 2.2630i 0.0153 + 0.0102i
Obj =
1.0e+11 *
5.0223 + 1.7757i 0.0230 + 0.0080i
Obj =
1.0e+11 *
8.4716 + 1.5626i 0.0385 + 0.0070i
Obj =
1.0e+11 *
8.2965 + 1.5221i 0.0377 + 0.0069i
Obj =
1.0e+11 *
3.3401 + 2.5507i 0.0155 + 0.0115i
Obj =
1.0e+12 *
1.3632 + 0.2196i 0.0062 + 0.0010i
Obj =
1.0e+11 *
2.7293 + 3.0808i 0.0127 + 0.0139i
Obj =
1.0e+11 *
7.1247 + 1.5626i 0.0325 + 0.0070i
Obj =
1.0e+11 *
4.0413 + 2.4315i 0.0186 + 0.0110i
Obj =
1.0e+11 *
7.3096 + 2.5849i 0.0333 + 0.0116i
Obj =
1.0e+11 *
3.7187 + 2.2086i 0.0171 + 0.0100i
Obj =
1.0e+11 *
2.9798 + 2.9953i 0.0139 + 0.0135i
Error using matlab.graphics.chart.primitive.Line/set
Complex values are not supported.
Error in gaplotpareto>plotFirstFront (line 84)
set(plotHandle,'Xdata',xy(:,1), 'Ydata',xy(:,2));
Error in gaplotpareto (line 28)
plotFirstFront(state.Score(range,:),state.Rank(range),markers{1 + mod(i,5)},objectivesToPlot(:)',flag,tag);
Error in gadsplot>callOnePlotFcn (line 194)
optimvalues = plotfcn(varargin{1:end});
Error in gadsplot (line 148)
[state,optimvalues] =
callOnePlotFcn(fname,plotNames{i},state,options.OutputPlotFcnOptions,optimvalues,flag,args{i}{:});
Error in gamultiobjsolve (line 64)
state = gadsplot(options,state,currentState,'Genetic Algorithm');
Error in gamultiobj (line 304)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in Main_Genetic_Algorithm (line 41)
[X, fval, exitflag, output, population, scores] = gamultiobj(@Fitness_Function, ...
Walter Roberson
Walter Roberson on 17 Jun 2021
You fix your objective function so that it never returns complex values, or you set up constraints so that you avoid the area in which it would return complex values. or you use some other kind of multi-objective minimizer that supports complex values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!