Error "Error using + Matrix dimensions must agree."

This is the programme:
function [ u ] = controller(~, s, s_des, params)
%PD_CONTROLLER PD controller for the height
%
% s: 2x1 vector containing the current state [z; v_z]
% s_des: 2x1 vector containing desired state [z; v_z]
% params: robot parameters
kv = 100;
kp = 100;
Z2=diff(s_des,2);
e = (s_des - s);
Y=diff(e);
u = params.mass*(Z2+kp*e+kv*Y+params.gravity);
% FILL IN YOUR CODE HERE
end
function [ sdot ] = sys_eom(t, s, controlhandle, trajhandle, params)
% sys_eom Differential equation for the height control system
s_des = trajhandle(t);
u_des = controlhandle(t, s, s_des, params);
u_clamped = min(max(params.u_min, u_des), params.u_max);
sdot = [s(2);
u_clamped/params.mass - params.gravity];
end
This is the run command
close all;
clear;
% Hover
z_des =1 ;
% Step
% z_des = 1;
% Given trajectory generator
trajhandle = @(t) fixed_set_point(t, z_des);
% This is your controller
controlhandle = @controller;
% Run simulation with given trajectory generator and controller
[t, z] = height_control(trajhandle, controlhandle);
% % Sample code to get more info on the response
% sim_info = lsiminfo(z, t, z_des);
% disp(['Settling time [s]: ', num2str(sim_info.SettlingTime)]);
% disp(['Overshoot [%]: ', num2str(max(0,(sim_info.Max-z_des)*100))]);

Answers (2)

Type the following at the MATLAB command line:
dbstop if error
Then run your code. It will pause at the line with the error. Examine all of the variables in that line for size to determine the cause of the error.
You have
Z2=diff(s_des,2);
where s_des has been documented to be 2 x 1. diff(s_des,2) is diff(diff(s_des)) and with s_des being length 2, the result is going to be empty. When you later use the result, Z2, in
u = params.mass*(Z2+kp*e+kv*Y+params.gravity)
that is going to lead to an error because you are trying to add an empty array and a non-empty item.

Categories

Find more on Control System Toolbox in Help Center and File Exchange

Tags

Asked:

on 2 Jul 2016

Edited:

on 2 Jul 2016

Community Treasure Hunt

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

Start Hunting!