How to list all results of probability experiment in Matlab [duplicate] - matlab

I am writing a function in Matlab to model the length of stay in hospital of stroke patients. I am having difficulty in storing my output values.
Here is my function:
function [] = losdf(age, strokeType, dest)
% function to mdetermine length of stay in hospitaal of stroke patients
% t = time since admission (days);
% age = age of patient;
% strokeType = 1. Haemorhagic, 2. Cerebral Infarction, 3. TIA;
% dest = 5.Death 6.Nursing Home 7. Usual Residence;
alpha1 = 6.63570;
beta1 = -0.03652;
alpha2 = -3.06931;
beta2 = 0.07153;
theta0 = -8.66118;
theta1 = 0.08801;
mu1 = 22.10156;
mu2 = 2.48820;
mu3 = 1.56162;
mu4 = 0;
nu1 = 0;
nu2 = 0;
nu3 = 1.27849;
nu4 = 0;
rho1 = 0;
rho2 = 11.76860;
rho3 = 3.41989;
rho4 = 63.92514;
for t = 1:1:365
p = (exp(-exp(theta0 + (theta1.*age))));
if strokeType == 1
initialstatevec = [1 0 0 0 0 0 0];
elseif strokeType == 2
initialstatevec = [0 1 0 0 0 0 0];
else
initialstatevec = [0 0 (1-p) p 0 0 0];
end
lambda1 = exp(alpha1 + (beta1.*age));
lambda2 = exp(alpha2 + (beta2.*age));
Q = [ -(lambda1+mu1+nu1+rho1) lambda1 0 0 mu1 nu1 rho1;
0 -(lambda2+mu2+nu2+rho2) lambda2 0 mu2 nu2 rho2;
0 0 -(mu3+nu3+rho3) 0 mu3 nu3 rho3;
0 0 0 -(mu4+nu4+rho4) mu4 nu4 rho4;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0];
Pt = expm(t./365.*Q);
Pt = Pt(strokeType, dest);
Ft = sum(initialstatevec.*Pt);
Ft
end
end
Then to run my function I use:
losdf(75,3,7)
I want to plot my values of Ft in a graph from from 0 to 365 days. What is the best way to do this?
Do I need to store the values in an array first and if so what is the best way to do this?

Many ways to do this, one straightforward way is to save each data point to a vector while in the loop and plot that vector after you exit your loop.
...
Ft = zeros(365,1); % Preallocate Ft as a vector of 365 zeros
for t = 1:365
...
Ft(t) = sum(initialstatevec.*Pt); % At index "t", store your output
...
end
plot(1:365,Ft);

Related

Extended Kalman Filter prediction update time

enter code hereI have implemented a Kalman filter for tracking moving objects using radar data. I have designed and generated data using the Matlab ADAS toolbox. After generating I have fed the data to the code which I have written in Matlab ( Filter - EKF, Gating - Euclidean Distance, Data Association- Probabilistic data association, initialization of tracks based on distance). So, now after executing, I have noticed that the prediction is not updating with respect to the detections. Its a bit slow compared to the detection inputs. Here an image below. So, the yellow one is the Kalman prediction and the red are the detections. So, because of late predictions and detections getting fast, the gating is coming out to be with zero detections to input to the PDA block. Due to this, the prediction is throwing NAN( Not a Number). And in order to solve this, the filter should fastly predict so we get enough detections for future predictions.
So, my question is how to do the predictions faulty associated with the detections.
This is the code
This is the predict function
function [TrackList, Evaluate] = predict( Evaluate,scans,TrackList,Par,associated_data,Track_no)
persistent prev
if (~isempty(TrackList{Track_no}))
if(scans>1)
TrackList{Track_no}.X = (TrackList{Track_no}.State_trans_mtx_F)*(TrackList{Track_no}.X);
else
prev = 0;
end
Evaluate.X(scans,:) = TrackList{Track_no}.X;
TrackList{Track_no}.range_A = sqrt((TrackList{Track_no}.X(1)^2)+(TrackList{Track_no}.X(4)^2));
TrackList{Track_no}.bearing_A = atan2(TrackList{Track_no}.X(4),TrackList{Track_no}.X(1));
TrackList{Track_no}.yhat = [TrackList{Track_no}.range_A TrackList{Track_no}.X(2) TrackList{Track_no}.X(3) TrackList{Track_no}.bearing_A TrackList{Track_no}.X(5) TrackList{Track_no}.X(6)];
Evaluate.Yhat(scans,:) = TrackList{Track_no}.yhat;
TrackList{Track_no}.meas_trans_mtx_H = [cos(TrackList{Track_no}.bearing_A) 0 0 sin(TrackList{Track_no}.bearing_A) 0 0 ;
0 1 0 0 0 0;
0 0 1 0 0 0;
-sin(TrackList{Track_no}.bearing_A)/TrackList{Track_no}.range_A 0 0 cos(TrackList{Track_no}.bearing_A)/TrackList{Track_no}.range_A 0 0;
0 0 0 0 1 0;
0 0 0 0 0 1];
Evaluate.meas_trans_mtx_H(scans).m = TrackList{Track_no}.meas_trans_mtx_H;
TrackList{Track_no}.Corr_mtx_error_P = (TrackList{Track_no}.State_trans_mtx_F)*(TrackList{Track_no}.Corr_mtx_error_P)*((TrackList{Track_no}.State_trans_mtx_F)') + Par.process_cov_Q;
Evaluate.Corr_mtx_error_P(scans).m = TrackList{Track_no}.Corr_mtx_error_P ;
TrackList{Track_no}.innovation_cov_S = (TrackList{Track_no}.meas_trans_mtx_H)*(TrackList{Track_no}.Corr_mtx_error_P)*(TrackList{Track_no}.meas_trans_mtx_H') + (TrackList{Track_no}.meas_cov_R);
Evaluate.innovation_cov_S(scans).m = TrackList{Track_no}.innovation_cov_S;
associated_meas.index = 1;
associated_meas.meas = [];
for k = 1:size(associated_data,1)
[associated_meas,dis] = gating(associated_meas,associated_data(k,:),TrackList{Track_no}.yhat,Par.gateLevel,TrackList{Track_no}.innovation_cov_S);
Evaluate.dis(k + prev) = dis;
end
prev = k + prev;
Evaluate.associated_data(scans).meas = associated_meas;
TrackList{Track_no}.associated_meas.index = associated_meas.index;
TrackList{Track_no}.associated_meas.meas = associated_meas.meas;
figure(1);
polarplot(TrackList{Track_no}.yhat(4),TrackList{Track_no}.yhat(1),'o');
end
end
This is the update function
function [TrackList,Evaluate] = update(Evaluate,TrackList,Par,Track_no,scans)
% for i = 1:size(TrackList,2)
if (~isempty(TrackList{Track_no})) %#ok<*ALIGN>
TrackList{Track_no}.Kalman_gain = (TrackList{Track_no}.Corr_mtx_error_P)*(TrackList{Track_no}.meas_trans_mtx_H')/(TrackList{Track_no}.innovation_cov_S);
Evaluate.Kalman_gain(scans).m = TrackList{Track_no}.Kalman_gain ;
% PDA
[innov,TrackList] = PDA(TrackList,Track_no);
Evaluate.innov(scans).m = innov;
% Update the state and covariance estimates
TrackList{Track_no}.Kalman_gain(3,3) = 0;
TrackList{Track_no}.Kalman_gain(6,6) = 0;
TrackList{Track_no}.X = TrackList{Track_no}.X + (TrackList{Track_no}.Kalman_gain)*innov';
Evaluate.updated_X(scans,:) = TrackList{Track_no}.X;
%%
TrackList{Track_no}.Vx_save(TrackList{Track_no}.count,1) = TrackList{Track_no}.X(2);
TrackList{Track_no}.Vy_save(TrackList{Track_no}.count,1) = TrackList{Track_no}.X(5);
% calculating acceleration from velocities
if(TrackList{Track_no}.count>2)
TrackList{Track_no}.X(3) = (TrackList{Track_no}.Vx_save(TrackList{Track_no}.count,1) - TrackList{Track_no}.Vx_save(TrackList{Track_no}.count-1,1))/Par.dt;
% Ax_save(count,1) = TrackList{i}.X(3);
TrackList{Track_no}.X(6) = (TrackList{Track_no}.Vy_save(TrackList{Track_no}.count,1) - TrackList{Track_no}.Vy_save(TrackList{Track_no}.count-1,1))/Par.dt;
% Ay_save(count,1)= TrackList{i}.X(6);
end
Evaluate.updated_X_acc(scans,:) = TrackList{Track_no}.X;
TrackList{Track_no}.Kalman_out = TrackList{Track_no}.X;
TrackList{Track_no}.count = TrackList{Track_no}.count + 1;
end
end
This is the track initialization
function [TrackList] = initialize(TrackList,clustered_data,Par,cluster_id,iterate,c11,c22)
if(iterate == 1)
for id = 1:cluster_id
TrackList = add_track(TrackList,clustered_data,Par,id,c11,c22);
end
else
for track_no = 1:size(TrackList,2)
n = 0;
for id = 1:cluster_id
[t,r] = cart2pol(TrackList{track_no}.X(1),TrackList{track_no}.X(4));
dis = pdist2([r t],[clustered_data(id).meas{1,1} clustered_data(id).meas{1,4}]);
if(dis < Par.gateLevel)
TrackList{track_no}.associated_data = clustered_data(id).meas;
end
if(dis > Par.gateLevel+10)
n = n+1;
if(n == size(TrackList,2))
TrackList = add_track(TrackList,clustered_data,Par,id,c11,c22);
TrackList{track_no}.associated_data = clustered_data(id).meas;
end
end
end
end
end
end
function TrackList = add_track(TrackList,clustered_data,Par,id,c11,c22)
Track_id = size(TrackList,2) + 1;
TrackList{Track_id}.meas.range = clustered_data(id).meas{1,1};
TrackList{Track_id}.meas.bearing = clustered_data(id).meas{1,4};
TrackList{Track_id}.meas.vx = clustered_data(id).meas{1,2};
TrackList{Track_id}.meas.vy = clustered_data(id).meas{1,5};
angle = clustered_data(id).meas{1,4};
range = clustered_data(id).meas{1,1};
% TrackList{Track_id}.meas_cov_R = [(Par.meas_std_range)^2*angle 0*(Par.meas_std_dis)*(Par.meas_std_vel) 0*(Par.meas_std_dis)*(Par.meas_std_acc) 0 0 0;
% 0*(Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 (Par.meas_std_vel)*(Par.meas_std_acc)*0 0 0 0;
% 0*(Par.meas_std_dis)*(Par.meas_std_acc) 0*(Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2 0 0 0;
% 0 0 0 (Par.meas_std_bearing)^2*angle 0*(Par.meas_std_dis)*(Par.meas_std_vel) 0*(Par.meas_std_dis)*(Par.meas_std_acc);
% 0 0 0 0*(Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 0*(Par.meas_std_vel)*(Par.meas_std_acc)
% 0 0 0 0*(Par.meas_std_dis)*(Par.meas_std_acc) 0*(Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2];
Par.meas_std_dis_x = Par.meas_std_range*(cos(angle)^2) + (range^2)*(sin(angle)^2)*Par.meas_std_bearing;
Par.meas_std_dis_y = Par.meas_std_range*(sin(angle)^2) + (range^2)*(cos(angle)^2)*Par.meas_std_bearing;
TrackList{Track_id}.meas_cov_R = [Par.meas_std_dis_x 0*(Par.meas_std_dis)*(Par.meas_std_vel) 0*(Par.meas_std_dis)*(Par.meas_std_acc) 0 0 0;
0*(Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 (Par.meas_std_vel)*(Par.meas_std_acc)*0 0 0 0;
0*(Par.meas_std_dis)*(Par.meas_std_acc) 0*(Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2 0 0 0;
0 0 0 Par.meas_std_dis_y 0*(Par.meas_std_dis)*(Par.meas_std_vel) 0*(Par.meas_std_dis)*(Par.meas_std_acc);
0 0 0 0*(Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 0*(Par.meas_std_vel)*(Par.meas_std_acc)
0 0 0 0*(Par.meas_std_dis)*(Par.meas_std_acc) 0*(Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2];
% TrackList{Track_id}.meas_cov_R = [(Par.meas_std_range)^2*angle (Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_dis)*(Par.meas_std_acc) 0 0 0;
% (Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 (Par.meas_std_vel)*(Par.meas_std_acc)*0 0 0 0;
% (Par.meas_std_dis)*(Par.meas_std_acc) (Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2 0 0 0;
% 0 0 0 (Par.meas_std_bearing)^2*angle (Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_dis)*(Par.meas_std_acc);
% 0 0 0 (Par.meas_std_dis)*(Par.meas_std_vel) (Par.meas_std_vel)^2 (Par.meas_std_vel)*(Par.meas_std_acc)
% 0 0 0 (Par.meas_std_dis)*(Par.meas_std_acc) (Par.meas_std_vel)*(Par.meas_std_acc) (Par.meas_std_acc)^2];
%
%TrackList{Track_id}.meas_cov_R = TrackList{Track_id}.meas_cov_R + 0.1*diag([1 0 1 1 0 1]);
TrackList{Track_id}.X = [clustered_data(id).meas{1,1}*cos(clustered_data(id).meas{1,4});clustered_data(id).meas{1,2};0;clustered_data(id).meas{1,1}*sin((clustered_data(id).meas{1,4}));clustered_data(id).meas{1,5};0];
% TrackList{Track_id}.X = [c11(id)+0.5;clustered_data(id).meas{1,2};0;c22(id)+0.5;clustered_data(id).meas{1,5};0];
TrackList{Track_id}.Corr_mtx_error_P = zeros(6,6);
TrackList{Track_id}.State_trans_mtx_F = [1 Par.dt 0.5*(Par.dt^2) 0 0 0;
0 1 Par.dt 0 0 0;
0 0 1 0 0 0;
0 0 0 1 Par.dt 0.5*(Par.dt^2);
0 0 0 0 1 Par.dt;
0 0 0 0 0 1];
TrackList{Track_id}.Vx_save = zeros(1,1);
TrackList{Track_id}.Vy_save = zeros(1,1);
TrackList{Track_id}.associated_data = clustered_data(id).meas;
TrackList{Track_id}.count = 1;
TrackList{Track_id}.death = 0;
TrackList{Track_id}.range_A = 0;
TrackList{Track_id}.bearing_A = 0;
TrackList{Track_id}.yhat = [0 0 0 0 0 0];
TrackList{Track_id}.meas_trans_mtx_H = zeros(6,6);
TrackList{Track_id}.Corr_mtx_error_P = zeros(6,6);
TrackList{Track_id}.innovation_cov_S = zeros(6,6);
TrackList{Track_id}.Kalman_gain = zeros(6,6);
TrackList{Track_id}.Vx_save = [];
TrackList{Track_id}.Vy_save = [];
TrackList{Track_id}.count = 1;
Par.process_cov_Q = [((Par.sig_ax^2)*(Par.dt^4))/4 ((Par.sig_ax^2)*(Par.dt^3))/2 ((Par.dt^2)/2)*(Par.sig_ax^2) 0 0 0;
((Par.sig_ax^2)*(Par.dt^3))/2 ((Par.sig_ax^2)*(Par.dt^2)) ((Par.sig_ax)^2)*Par.dt 0 0 0;
((Par.dt^2)/2)*(Par.sig_ax^2) ((Par.sig_ax)^2)*Par.dt (Par.sig_ax)^2 0 0 0;
0 0 0 ((Par.sig_ay^2)*(Par.dt^4))/4 ((Par.sig_ay^2)*(Par.dt^3))/2 ((Par.dt^2)/2)*(Par.sig_ay^2);
0 0 0 ((Par.sig_ay^2)*(Par.dt^3))/2 ((Par.sig_ay^2)*(Par.dt^2)) ((Par.sig_ay)^2)*Par.dt;
0 0 0 ((Par.dt^2)/2)*(Par.sig_ay^2) ((Par.sig_ay)^2)*Par.dt (Par.sig_ay)^2 ];
end
Thank You

Undefined function or variable for 3dos mechanism

I tried to make a 3dof(3 degrees of freedom mechanism) in matlab but i get this error and i don't know why.
this is for a school project and i need to simulate a human finger.
the code is running normal but after i enter the values for the angles it says that A,B,C are
undefined and i don't know why
a1 = input('valuarea lui q1(grade):');
a2 = input('valuarea lui q2(grade):');
a3 = input('valuarea lui q3(grade):');
L1=35;
L2=45;
L3=30;
z = [-10 10];
plot(z,10);
grid ON;
O=[0;0;0;1];
m= linspace(pi/2,pi/2+a1*pi/180,100);
n = linspace(-pi/2,a2*pi/180,100);
k=linspace(-pi/2,a3*pi/180,100);
for a=1:100
[A1,B1,C1] = Transform(m(a),n(a),k(a),L1,L2,L3);
x = [O(1) A(1) B(1) C(1)];
y = [O(2) A(2) B(2) C(2)];
Cx(i)= C1(1);
Cy(i) = C1(2);
i=i+1;
Plot = plot(x,y,'r',...1
'LineWidth',1);
title('Sumularea unui deget');
plot(Cx,Cy,'--g',...
'LineWidth',1);
pause(0.075);
delete(Plot);
end
plot(x,y,'r',...
'LineWidth',3);
function [A,B,C ] = Transform( m,n,p,l1,l2,l3 )
P = [0;0;0;1];
T1 = [cos(m) -sin(m) 0 0;sin(m) cos(m) 0 0;0 0 1 0; 0 0 0 1];
T2 = [cos(n) -sin(n) 0 11;sin(n) cos(n) 0 0;0 0 1 0; 0 0 0 1];
T3 = [cos(p) -sin(p) 0 12;sin(p) cos(p) 0 0;0 0 1 0; 0 0 0 1];
T4 = [1 0 0 13;0 1 0 0; 0 0 1 0; 0 0 0 1];
A = T1*T2*P;
B = T1*T2*T3*P;
C = T1*T2*T3*T3*P;
end
In your main function, this is the first use of the variables:
x = [O(1) A(1) B(1) C(1)];
They are never written previously. Instead A1 is written, which is a different variable. I guess you mixed the two up.

How to find the centroids of pixel clusters within a binary image

I've written some code in MATLAB that converts an image (of stars) into a binary image using a set threshold and then labels each cluster of pixels (stars) that is above this threshold. The labelling produces an output:
e.g.
[1 1 1 0 0 0 0 0 0
1 1 0 0 0 2 2 2 0
0 0 0 3 3 0 2 0 0
0 0 0 3 3 0 0 0 0]
So each cluster of 1's, 2's, 3's etc. represents a star. I used the answer provided at this link: How to find all connected components in a binary image in Matlab? to label the pixels.
I also can't use the image processing toolbox.
The code that I have so far is shown below.
How do I now find the centroids of each pixel cluster in the image?
clc
clear all
close all
img=imread('star.jpg');
binary_image=convert2binary(img);
imshow(binary_image);
visited = false(size(binary_image));
[rows, cols] = size(binary_image);
B = zeros(rows, cols);
ID_counter = 1;
for row = 1:rows
for col = 1:cols
if binary_image(row, col) == 0
visited(row, col) = true;
elseif visited(row, col)
continue;
else
stack = [row col];
while ~isempty(stack)
loc = stack(1,:);
stack(1,:) = [];
if visited(loc(1),loc(2))
continue;
end
visited(loc(1),loc(2)) = true;
B(loc(1),loc(2)) = ID_counter;
[locs_y, locs_x] = meshgrid(loc(2)-1:loc(2)+1, loc(1)-1:loc(1)+1);
locs_y = locs_y(:);
locs_x = locs_x(:);
out_of_bounds = locs_x < 1 | locs_x > rows | locs_y < 1 | locs_y > cols;
locs_y(out_of_bounds) = [];
locs_x(out_of_bounds) = [];
is_visited = visited(sub2ind([rows cols], locs_x, locs_y));
locs_y(is_visited) = [];
locs_x(is_visited) = [];
is_1 = binary_image(sub2ind([rows cols], locs_x, locs_y));
locs_y(~is_1) = [];
locs_x(~is_1) = [];
stack = [stack; [locs_x locs_y]];
end
ID_counter = ID_counter + 1;
end
end
end
function [binary] = convert2binary(img)
[x, y, z]=size(img);
if z==3
img=rgb2gray(img);
end
img=double(img);
sum=0;
for i=1:x
for j=1:y
sum=sum+img(i, j);
end
end
threshold=100 % or sum/(x*y);
binary=zeros(x,y);
for i=1:x
for j=1:y
if img(i, j) >= threshold
binary(i, j) = 1;
else
binary(i, j)=0;
end
end
end
end
The centroid is the first order moment. It is computed by
sum(x*v)/sum(v) , sum(y*v)/sum(v)
For a binary image you can do this (I'm using a trivial loop, not vectorized code, so we can extend it later easily):
img = [1 1 1 0 0 0 0 0 0
1 1 0 0 0 2 2 2 0
0 0 0 3 3 0 2 0 0
0 0 0 3 3 0 0 0 0]; % Op's example data
bin = img==1; % A binary image
% Algorithm
sum_v = 0;
sum_iv = 0;
sum_jv = 0;
for jj=1:size(bin,2)
for ii=1:size(bin,1)
sum_v = sum_v + bin(ii,jj);
sum_iv = sum_iv + ii * bin(ii,jj);
sum_jv = sum_jv + jj * bin(ii,jj);
end
end
centroid = [sum_iv, sum_jv] / sum_v;
You could of course iterate over each of the labels of the labeled image img, and apply the above code. But that is highly inefficient. Instead, we can loop through the image once and compute all centroids at the same time. We convert sum_v etc. into vectors, containing one running sum per label:
N = max(img(:)); % number of labels
sum_v = zeros(N,1);
sum_iv = zeros(N,1);
sum_jv = zeros(N,1);
for jj=1:size(img,2)
for ii=1:size(img,1)
index = img(ii,jj);
if index>0
sum_v(index) = sum_v(index) + 1;
sum_iv(index) = sum_iv(index) + ii;
sum_jv(index) = sum_jv(index) + jj;
end
end
end
centroids = [sum_iv, sum_jv] ./ sum_v;

MATLAB Yalmip: unable to run optimizer; error in eliminatevariables

Bottomline:
Matlab throws the errors below and it is not obvious to me what is the root cause. The problem seems to reside in the input arguments, but I cannot figure out exactly what it is. I would greatly appreciate any help in finding it.
Index exceeds matrix dimensions.
Error in eliminatevariables (line 42)
aux(model.precalc.index2) = value(model.precalc.jj2);
Error in optimizer/subsref (line 276)
[self.model,keptvariablesIndex] =
eliminatevariables(self.model,self.model.parameterIndex,thisData(:),self.model.parameterIndex);
Error in SCMv0_justrun (line 68)
[solutions,diagnostics] = controller{inputs};
Background:
I am trying to program a Model Predictive Control but I am not very familiar yet with either Yalmip or mathematical optimization algorithms. I have made sure that the defined inputs and the actual inputs have the same dimensions, hence why I am surprised that the error has to do with matrix dimensions.
The error originates in when my code calls the optimizer.
My code is based on: https://yalmip.github.io/example/standardmpc/
Here is my code (the first part of the code is only needed to define the optimization problem and is marked between "%%%%%"; the error occurs near the end):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yalmip('clear')
clear all
% Model data
A = eye(3);
B = [1 0 -1 0 0 0 0; 0 1 0 -1 0 0 0; 0 0 0 0 1 -1 -1];
nx = 3; % Number of states
nu = 7; % Number of inputs
% MPC data
Q = [10 10 20]';
R = [10 10 1 1 5 3 3]';
C = [50 0; 0 30];
N = 90;
ny = 2;
E = [0 0 0 0 0 1 0; 0 0 0 0 0 0 1];
u = sdpvar(repmat(nu,1,N),repmat(1,1,N));
x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1));
r = sdpvar(repmat(ny,1,N+1),repmat(1,1,N+1));
d = sdpvar(ny,1);
pastu = sdpvar(nu,1);
dx = 0.05;
Gx=[-1*eye(3);eye(3)];
gx = [0 0 0 500 500 1000]';
COVd = [zeros(5,7);0 0 0 0 0 10 0; 0 0 0 0 0 0 10];
COVx = zeros(nx,nx);
auxa = eye(5);
auxb = zeros(5,2);
Gu = [-1*eye(7,7); auxa auxb;0 0 0 0 0 1 1];
gu = [zeros(7,1); 200; 200; 50; 50; 100; 500];
Ga = [0 0 0.5 0.5 -1 0 0];
constraints = [];
objective = 0;
for k = 1:N
r{k} = r{k} + d;
objective = objective + Q'*x{k} + R'*u{k} + (r{k}-E*u{k})'*C*(r{k}-E*u{k});
constraints = [constraints, x{k+1} == A*x{k}+B*u{k}];
COVx = A*COVx*A' + B*COVd*B';
COVGx = Gx*COVx*Gx';
StDevGx = sqrt(diag(COVGx));
chance = gx - norminv(1-dx/(length (gx)*N))*StDevGx;
constraints = [constraints, Ga*u{k}==0, Gu*u{k}<=gu, Gx*x{k}<=gx-chance];
end
objective = objective + Q'*x{N+1};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parameters_in = {x{1},[r{:}],d,pastu};
solutions_out = {[u{:}], [x{:}]};
controller = optimizer(constraints, objective,[],parameters_in,solutions_out);
x = 100*ones(nx,1);
clf;
disturbance = randn(ny,1)*10;
oldu = zeros(nu,1);
hold on
for i = 1:150
future_r = [4*sin((i:i+N)/40);3*sin((i:i+N)/20)];% match dimensions of r
inputs = {x,future_r,disturbance,oldu};
[solutions,diagnostics] = controller{inputs};
U = solutions{1};oldu = U(1);
X = solutions{2};
if diagnostics == 1
error('The problem is infeasible');
end
x = A*x+B*u;
end
It's a bug in the latest version of YALMIP.

"Contour not rendered for non-finite ZData"

I'm trying to plot a frequency characteristic equation using ezplot, but Matlab gives the following warning, "Contour not rendered for non-finite ZData". I have used this command to plot frequency equations previously but now I get a warning and the plot display is empty and it does not change the axis range as well. Can someone please help. Would be much appreciated.
Here's the code i'm using.
% Transfer Matrix for Case-I, thin rotor
clear all;
clc;
EI = 1626;
l = 0.15;
m = 0.44108;
It = 2.178*10^-4;
I_p = 2.205*10^-5;
Itr = 0.24;
I_pr = 0.479;
syms p n;
F = [1 l*1i l^2/(2*EI)*1i l^3/(6*EI);
0 1 l/EI -l^2/(2*EI)*1i;
0 0 1 -l*1i;
0 0 0 l];
P = [ 1 0 0 0;
0 1 0 0;
0 -It*p^2+I_p*n*p 1 0;
-m*p^2 0 0 1];
P_r = [1 0 0 0;
0 1 0 0;
0 -Itr*p^2+I_pr*n*p 1 0;
-m*p^2 0 0 1];
A = F*P*F*P*F*P*F;
B = P_r*F*P*F*P*F;
r = A(1,2)/A(1,4);
a12_p = 0;
a22_p = A(2,2)-r*A(2,4);
a32_p = A(3,2)-r*A(3,4);
a42_p = A(4,2)-r*A(4,4);
Ap(2,2) = a22_p;
Ap(3,2) = a32_p;
Ap(4,2) = a42_p;
Ap(4,4) = 1;
C = B*Ap;
M = [C(3,2) C(3,4);
C(4,2) C(4,4)];
sol = det(M);
ezplot(sol,[-2*10^10 2*10^10]);
The sol is displayed if u ask for it but the plot doesn't display.
Thanks in advance for ur help !! Much appreciated.

Categories