I use fcn2optimexpr,but I got an error for scalar optimizationexpression

조회 수: 3 (최근 30일)
rng default
A = [2,3];
a = [-0.9,-2.9];
tdata = 3*rand(200,1);
tdata = sort(tdata); % Increasing times for easier plotting
noisydata=0.07*randn(size(tdata))
ydata = A.*exp(a(1).*tdata).*cos(a(2).*tdata) + noisydata ;
plot(tdata,ydata,'b*')
xlabel 't'
ylabel 'Response'
A = optimvar('A',2);
a = optimvar('a',2);
objfunction =@(A,a)A.*exp(a(1).*tdata).*cos(a(2).*tdata);
response = fcn2optimexpr(objfunction,A,a,'OutputSize',[200,2],"ReuseEvaluation",true)
obj = sum((response - ydata).^2);
%%%%%%%%error appear when run the optimproblem
lsqprob = optimproblem;
lsqprob.Objective=obj
x0.A = [1/2,3/2];
x0.a = [-1/2,-3/2];
show(lsqprob)
[sol ,fval] = solve(lsqprob,x0)
I gor error for optimproblem -->Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression.
another problem is that solve can not call

채택된 답변

Alan Weiss
Alan Weiss 2022년 5월 8일
You had a misunderstanding. The A variable should be a scalar, not a vector. All issues stemmed from that one misunderstanding.
rng default
A = 2; % Was [2,3]
a = [-0.9,-2.9];
tdata = 3*rand(200,1);
tdata = sort(tdata); % Increasing times for easier plotting
noisydata=0.07*randn(size(tdata));
ydata = A.*exp(a(1).*tdata).*cos(a(2).*tdata) + noisydata ;
plot(tdata,ydata,'b*')
xlabel 't'
ylabel 'Response'
A = optimvar('A'); % used to be of size 2
a = optimvar('a',2);
objfunction =@(A,a)A.*exp(a(1).*tdata).*cos(a(2).*tdata);
response = fcn2optimexpr(objfunction,A,a,"ReuseEvaluation",true) % 200-by-1, not 200-by-2
response =
200×1 Nonlinear OptimizationExpression array with properties: IndexNames: {{} {}} Variables: [1×1 struct] containing 2 OptimizationVariables See expression formulation with show.
obj = sum((response - ydata).^2);
%%%%%%%%error appear when run the optimproblem
lsqprob = optimproblem;
lsqprob.Objective=obj;
x0.A = 1/2; % was [1/2 3/2]
x0.a = [-1/2,-3/2];
show(lsqprob)
OptimizationProblem : Solve for: A, a minimize : sum((((A .* exp((a(1) .* extraParams{1}))) .* cos((a(2) .* extraParams{2}))) - extraParams{3}).^2) extraParams{1}: 0.0139 0.0357 0.0462 0.0955 0.1033 0.1071 0.1291 0.1385 0.1490 0.1619 0.1793 0.2276 0.2279 0.2345 0.2434 0.2515 0.2533 0.2894 0.2914 0.2926 0.3200 0.3336 0.3570 0.3700 0.3810 0.3897 0.3959 0.4082 0.4159 0.4257 0.4349 0.4366 0.4479 0.4571 0.4728 0.4865 0.4878 0.4969 0.5070 0.5136 0.5455 0.5505 0.5517 0.5606 0.5669 0.5898 0.6714 0.6869 0.7043 0.7197 0.7199 0.7251 0.7306 0.7533 0.7628 0.7653 0.7725 0.7796 0.7889 0.7914 0.8281 0.8308 0.8355 0.8575 0.8890 0.9190 0.9336 0.9513 1.0114 1.0132 1.0212 1.0500 1.0529 1.0550 1.0595 1.1055 1.1077 1.1413 1.1447 1.1692 1.1767 1.1993 1.2054 1.2117 1.2518 1.2653 1.2942 1.3076 1.3162 1.3280 1.3368 1.3404 1.3516 1.3528 1.4082 1.4199 1.4561 1.4604 1.4678 1.4693 1.4726 1.4951 1.5179 1.5255 1.5323 1.5397 1.5856 1.5924 1.6150 1.6406 1.6410 1.6416 1.6492 1.6496 1.7035 1.7065 1.7256 1.7391 1.7558 1.7558 1.8059 1.8481 1.8662 1.8769 1.8971 1.9389 1.9432 1.9473 1.9622 1.9653 1.9664 1.9672 2.0362 2.0391 2.0603 2.0676 2.0845 2.0972 2.1181 2.1281 2.1952 2.2294 2.2341 2.2445 2.2538 2.2612 2.2641 2.2716 2.2732 2.2966 2.3247 2.3271 2.3375 2.3407 2.3408 2.3766 2.3829 2.3845 2.3856 2.4002 2.4008 2.4429 2.4442 2.4519 2.4529 2.4636 2.4704 2.4775 2.4925 2.5222 2.5474 2.5591 2.6061 2.6079 2.6727 2.7002 2.7081 2.7174 2.7319 2.7400 2.7401 2.7472 2.7516 2.7878 2.7882 2.8020 2.8020 2.8262 2.8344 2.8507 2.8684 2.8715 2.8725 2.8779 2.8785 2.8792 2.8857 2.8947 2.9118 2.9884 extraParams{2}: 0.0139 0.0357 0.0462 0.0955 0.1033 0.1071 0.1291 0.1385 0.1490 0.1619 0.1793 0.2276 0.2279 0.2345 0.2434 0.2515 0.2533 0.2894 0.2914 0.2926 0.3200 0.3336 0.3570 0.3700 0.3810 0.3897 0.3959 0.4082 0.4159 0.4257 0.4349 0.4366 0.4479 0.4571 0.4728 0.4865 0.4878 0.4969 0.5070 0.5136 0.5455 0.5505 0.5517 0.5606 0.5669 0.5898 0.6714 0.6869 0.7043 0.7197 0.7199 0.7251 0.7306 0.7533 0.7628 0.7653 0.7725 0.7796 0.7889 0.7914 0.8281 0.8308 0.8355 0.8575 0.8890 0.9190 0.9336 0.9513 1.0114 1.0132 1.0212 1.0500 1.0529 1.0550 1.0595 1.1055 1.1077 1.1413 1.1447 1.1692 1.1767 1.1993 1.2054 1.2117 1.2518 1.2653 1.2942 1.3076 1.3162 1.3280 1.3368 1.3404 1.3516 1.3528 1.4082 1.4199 1.4561 1.4604 1.4678 1.4693 1.4726 1.4951 1.5179 1.5255 1.5323 1.5397 1.5856 1.5924 1.6150 1.6406 1.6410 1.6416 1.6492 1.6496 1.7035 1.7065 1.7256 1.7391 1.7558 1.7558 1.8059 1.8481 1.8662 1.8769 1.8971 1.9389 1.9432 1.9473 1.9622 1.9653 1.9664 1.9672 2.0362 2.0391 2.0603 2.0676 2.0845 2.0972 2.1181 2.1281 2.1952 2.2294 2.2341 2.2445 2.2538 2.2612 2.2641 2.2716 2.2732 2.2966 2.3247 2.3271 2.3375 2.3407 2.3408 2.3766 2.3829 2.3845 2.3856 2.4002 2.4008 2.4429 2.4442 2.4519 2.4529 2.4636 2.4704 2.4775 2.4925 2.5222 2.5474 2.5591 2.6061 2.6079 2.6727 2.7002 2.7081 2.7174 2.7319 2.7400 2.7401 2.7472 2.7516 2.7878 2.7882 2.8020 2.8020 2.8262 2.8344 2.8507 2.8684 2.8715 2.8725 2.8779 2.8785 2.8792 2.8857 2.8947 2.9118 2.9884 extraParams{3}: 2.0062 1.9117 1.9451 1.7782 1.6691 1.7957 1.6789 1.6345 1.6244 1.5602 1.4110 1.2761 1.2759 1.2219 1.3405 1.1280 1.1478 0.9800 0.9390 1.0025 0.8799 0.9476 0.7227 0.6102 0.7500 0.6871 0.5583 0.4173 0.4596 0.4391 0.4314 0.3866 0.3900 0.3491 0.1715 0.1390 0.1485 0.1298 0.1048 0.1034 -0.2258 -0.0634 0.0514 -0.1410 -0.0224 -0.1391 -0.4037 -0.4280 -0.5918 -0.5227 -0.4047 -0.5213 -0.5369 -0.6361 -0.6045 -0.5904 -0.5894 -0.6573 -0.6627 -0.5084 -0.8589 -0.5481 -0.6861 -0.6632 -0.8761 -0.8185 -0.8026 -0.7585 -0.9043 -0.7540 -0.8698 -0.7691 -0.7267 -0.7484 -0.6931 -0.6676 -0.7817 -0.6879 -0.7687 -0.7692 -0.6036 -0.6415 -0.6378 -0.5622 -0.5308 -0.5288 -0.4233 -0.4256 -0.4606 -0.5075 -0.4911 -0.3564 -0.5349 -0.4219 -0.4676 -0.2408 -0.1933 -0.2467 -0.2399 -0.4066 -0.1866 -0.3450 -0.3183 -0.1385 -0.2001 -0.0936 -0.0073 0.0150 -0.0618 0.0522 0.0283 0.0799 0.0693 0.0952 0.0882 0.0905 0.1923 -0.0128 0.1172 0.0636 0.1705 0.2739 0.2979 0.1754 0.2244 0.2855 0.2570 0.3000 0.3117 0.2195 0.2725 0.1359 0.3776 0.2536 0.2141 0.2806 0.1977 0.2953 0.2551 0.4452 0.3561 0.0897 0.2936 0.1607 0.2369 0.2631 0.3027 0.2279 0.3566 0.2017 0.2442 0.2666 0.2206 0.2746 0.2355 0.1383 0.0633 0.3188 0.1458 0.1869 0.2187 0.1624 0.0903 0.1162 0.1397 0.2454 0.0775 0.1884 0.1459 0.0898 0.0169 0.0639 0.0499 -0.0479 0.0320 -0.0534 -0.0065 0.0190 -0.0751 -0.0358 0.0088 -0.1475 0.0515 0.1325 0.0298 -0.0652 -0.0131 -0.1252 0.0757 0.0041 -0.0127 -0.1301 -0.0467 -0.1099 -0.0928 -0.1112 -0.1451 -0.1397 -0.0957 -0.2175
[sol ,fval] = solve(lsqprob,x0)
Solving problem using lsqnonlin. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol = struct with fields:
A: 2.0035 a: [2×1 double]
fval = 0.9375
Alan Weiss
MATLAB mathematical toolbox documentation

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Problem-Based Optimization and Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by