Find values of a function in the same region and save them into different arrays for each region
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I have created a meshgrid divided in 13x17 regions and I have a function that travels through this region. This function that iterates in a for loop is an array and what I want to do is create different arrays that groups the values of this function if they are in the same region. Sorry if I'm not explaining very well, english is not my first language and I its a complex work for me. I attached an image that shows this function in red travelling through the grid.
I'm having a big trouble trying to separate those values from the function and grouping them in other arrays if they meet a condition (the condition is to be in the same region of the grid). Anyone who can help me?
Attached code:
%% this script evaluates the response of two second order functions and identifies the plant using a look up table
%% INIT
clear variables;
close all;
clc;
%% variable initialization
% time
time=150;
deltaT=0.01;
t = (0:deltaT:time);
%% Reference
A=2;
w=0.8;
r=A*sin(w*t);
rd=gradient(r,t);
[a,b,x1init,x2init,d1,d2,w1,w2,w12,w22,d1sigma,d1sigma2,d2tau,d2tau2,mup,x1p,x2p,fintp,x1d,x2d,fintd,x1cp,x2cp,fintcp] = initVar(t);
% initial conditions
x1p(1)=x1init; %2
x2p(1)=x2init;
x1d(1)=x1init;
x2d(1)=x2init;
Kp=3;
Kd=0.8;
%% SOLVE SYSTEM AND CREATE THE PLANE
% fd function for desired plant PD
fd=@(pd1,pd2)(a*pd1+b*pd2);
% fp function for van der pol system plant (P plant)
fp=@(fp1,fp2)(mup.*(1-(fp1.^2)).*fp2-fp1);
% define working area to plot the plane
[X,Y]=meshgrid(d1,d2);
%values of both functions x2prima at vertexes
PDLUT= fd(X, Y)
PLUT= fp(X, Y)
%Controller plant LUT is calculated directly
CLUT=PDLUT-PLUT;
% dimensions of the PB model
X1=X(1,:);
X2=Y(:,1);
%% LOOP
% interpolates the system using the working area X Y, LUT table(f at
% vertexese) and the pos and speed at instant (x1p, x2p, x1d, x2d) (emulates Ode45)
nIterations=length(t);
x1dError=zeros(1,length(t));
x2dError=zeros(1,length(t));
x1pError=zeros(1,length(t));
x2pError=zeros(1,length(t));
u=zeros(1,length(t));
ud=zeros(1,length(t));
up=zeros(1,length(t));
F=zeros(length(t),1);
Xmat=zeros(length(t),4);
phi=zeros(4,1);
tic
for ind2=2:nIterations
%% P PLANT + CP PLANT
% introduce reference signal
r1=r(1,ind2-1);
rd1=rd(1,ind2-1);
% if the function is between conditions, then the controller C starts
% working.
% using the C controller plant values and P plant observations (x1p, x2p), interpolates the value of U
u(1,ind2)=interp2(X,Y,CLUT,x1p(1,ind2-1),x2p(1,ind2-1));
% calculate response of van der pol system:
% 1.- first interpolates fp funtion value using the last observations
% of the P plant (x1pant, x2pant)(initial values in the first loop x1,x2=2)
% 2.- using u and the new f, calculates the next observations of the P
% plant.
[x1p(1,ind2),x2p(1,ind2),fintp(1,ind2)]=ffunc(X,Y,PLUT,x1p(1,ind2-1),x2p(1,ind2-1),deltaT,u(1,ind2));
% Position and velocity error calculated
x1pError(1,ind2)=r1-x1p(1,ind2-1);
x2pError(1,ind2)=rd1-x2p(1,ind2-1);
% PID output "U"
ufb(1,ind2)= Kp*x1pError(1,ind2)+Kd*x2pError(1,ind2);
[x1p(1,ind2),x2p(1,ind2),fintp(1,ind2)]=fCP(X,Y,PLUT,x1p(1,ind2-1),x2p(1,ind2-1),deltaT,u(1,ind2),ufb(1,ind2-1));
%% PD PLANT
% Position and velocity error calculated
x1dError(1,ind2)=r1-x1d(1,ind2-1);
x2dError(1,ind2)=rd1-x2d(1,ind2-1);
% PID output "U"
ud(1,ind2)= Kp*x1dError(1,ind2)+Kd*x2dError(1,ind2);
% calculate response of desired plant:
% 1.- first interpolates fd funtion value using the last observations
% of the PD plant (x1dant, x2dant)(initial values in the first loop x1,x2=2)
% 2.- using u and the new f, calculates the next observations of the DP
% plant.
[x1d(1,ind2),x2d(1,ind2),fintd(1,ind2)]=ffunc(X,Y,PDLUT,x1d(1,ind2-1),x2d(1,ind2-1),deltaT,ud(1,ind2));
%% CP PLANT
x1cp=x1p;
x2cp=x2p;
%calc x1cp and x2cp
% get the f function of the CP close loop plant
fintcp(1,ind2)= fintp(1,ind2)+ u(1,ind2);
%% Calculation of weights alpha and beta
%find in which part of the table corresponds x1
X1find=find(X1<x1cp(1,ind2));
X1ind=X1find(end);
d1sigma(1,ind2)=X1(X1ind);
d1sigma2(1,ind2)=X1(X1ind+1);
%find in which part of the table corresponds x2
X2find=find(X2<x2cp(1,ind2));
X2ind=X2find(end);
d2tau(1,ind2)=X2(X2ind);
d2tau2(1,ind2)=X2(X2ind+1);
%aisle values of fintcp from a certain region (HERE THE ISSUE)
% I want to save all those values of fintcp that are in the same region
% into different arrays for each region
%calculate alpha and beta
w1(1,ind2)=(x1cp(1,ind2)-d1sigma(1,ind2))/(d1sigma2(1,ind2)-d1sigma(1,ind2));
w2(1,ind2)=(x2cp(1,ind2)-d2tau(1,ind2))/(d2tau2(1,ind2)-d2tau(1,ind2));
w12(1,ind2)=1-w1(1,ind2);
w22(1,ind2)=1-w2(1,ind2);
end
toc
phi=inv(Xmat'*Xmat)*Xmat'*F;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%FUNCTION
function [x1act,x2act,f]= ffunc(X,Y,LUT,x1ant,x2ant,deltaT,u)
% calculate f(x1,x2) with the vertexes (LUT, X and Y) and instant
% pos/speed(x1,x2)
f=interp2(X,Y,LUT,x1ant,x2ant);
x1act= deltaT*x2ant+x1ant;
x2act=(f+u)*deltaT+x2ant;
end
%%%%%%%%%%%
function [x1act,x2act,f]= fCP(X,Y,LUT,x1ant,x2ant,deltaT,u,r)
% calculate f(x1,x2) with the vertexes (LUT, X and Y) and instant
% pos/speed(x1,x2)
f=interp2(X,Y,LUT,x1ant,x2ant);
x1act= deltaT*x2ant+x1ant;
x2act=(f+u+r)*deltaT+x2ant;
end
%%%%%%%%%%
%INITIALIZATION
function [a,b,x1init,x2init,d1,d2,w1,w2,w12,w22,d1sigma,d1sigma2,d2tau,d2tau2,mup,x1p,x2p,fintp,x1d,x2d,fintd,x1cp,x2cp,fintcp] = initVar(t)
% a refers to realtion of electromotive force and the rotor inertia a=K/J
% b refers to relation of viscuous friction parameter and the rotor b=-b/J
% inertia
a=-0.3;
b=-1.1;
%initial conditions
x1init= 2;
x2init= 2;
% vertex
d1=(-4:0.5:4);
d2=(-3:0.5:3);
w1=zeros(1,length(t));
w2=zeros(1,length(t));
w12=zeros(1,length(t));
w22=zeros(1,length(t));
d1sigma=zeros(1,length(t));
d1sigma2=zeros(1,length(t));
d2tau=zeros(1,length(t));
d2tau2=zeros(1,length(t));
%van der pol inestability and damping force parameter
mup=1;
%van der pol position and speed vector init
x1p=nan(1,length(t));
x2p=nan(1,length(t));
x1d=nan(1,length(t));
x2d=nan(1,length(t));
x1cp=nan(1,length(t));
x2cp=nan(1,length(t));
fintd=nan(1,length(t));
fintp=nan(1,length(t));
fintcp=nan(1,length(t));
end
%%%%%%%%
댓글 수: 1
Matt Butts
2022년 9월 21일
I am not sure I completely understand your question, but it seems like histcounts2 might be able to help. This could tell you which bin each point belongs to in your grid.
[~,~,~,binX,binY] = histcounts2(xFuncVals,yFuncVals,xEdges,yEdges);
% Points in 1,1 of mesh
xFuncVals(binX == 1 & binY == 1)
yFuncVals(binX == 1 & binY == 1)
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!