Return vector that minimizes a function

조회 수: 2 (최근 30일)
Loic Van Aelst
Loic Van Aelst 2016년 10월 30일
답변: Keerthana Chivukula 2016년 11월 4일
Hello,
I am writing an iterative algorithm that updates a vector "u" each iteration step. I also keep the previous vectors u, so I can control what happened previously, so each iteration there is a new row added to u. This row is the latest vector u if you want. So each iteration I try to update the latest u(k,:) I have by a new u(k+1,:).
The problem is not this iteration, but in each iteration step I find the new u(k+1,:) by minimizing a function "fun". However, this gives an error: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
x0 = u(k,:);
fun = @(x)(r - (y0 + T.*x)).^2 + x-u(k,:);
u(k+1,:) = fminsearch(fun, x0);
Here is the full code: %% ILC algoritme clear all; close all; clc;
% Begin input: assistentie exoskeleton
u = zeros(1,5); %Begin met zero assistentie of je kan al een goede initiele guess maken eens je het systeem van het OpenSim model kent
max_it = 100; % Kwestie van niet te lang zoeken
lambda = 0.5; % Nu is dit nog een gewone constante, later verbeterd!
y0 = [1 0 -1 3 4];
y= [];
r = 5* ones(1,5);
e = [];
Norm = [];
T= [1 5 3 4 5];
for k = 1:1:max_it
y = [y ; y0 + T.*u(k,:)]; % Vervangen door Torque_driven model met u als input voor exoskeleton
e= [e ; r - y(k,:)];
%e(k+1,:)= r - (y0 + T.*u(k+1,:));
Norm = [ Norm ; norm(e(k,:))];
if Norm(k) < 0.1
break
end
x0 = u(k,:);
fun = @(x)(r - (y0 + T.*x)).^2 + x-u(k,:);
u(k+1,:) = fminsearch(fun, x0);
end
figure
xlabel('Time_samples')
ylabel('Norm error')
title('Norm error in time')
plot(Norm)

채택된 답변

Keerthana Chivukula
Keerthana Chivukula 2016년 11월 4일
The underlying reason for this error message is that the output expected from the objective function provided to "fminsearch" is supposed to be a scalar as mentioned in the following link:
However, in the code provided above, the function "fun" returned a matrix and this resulted in incorrect indexing. Modifying the function to return a scalar would fix the issue.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by