Evaluate gradient function in the for loop.

4 vues (au cours des 30 derniers jours)
HN
HN le 14 Août 2020
Modifié(e) : HN le 22 Août 2020
is a function dependent on time. It is n the function and need to be differentiated to get and the differentiation should pass through the for loop. But for loop picks one value at time t and differentiation failed. Code attached.
function S = Get_Vel(t)
ts=0.0001;
x(t)=cos(2*pi*t)
y(t)=sin(2*pi*t)
vx(t)=gradient(x,ts)
vy(t)=gradient(y,ts)
S=[vx;vy]
end
function A = Compute(t,ts,~,~,~)
S=Get_Vel(t)
end
function solve = solver(F,t0,tf,y0,~)
for t=t0:ts:tf-ts
A =F(t,ts,~,~)
end
solve =A
end
%% MAIN
Result=solver(@Compute,t0,tf,y0,~)
But, since solver used a for loop gradient failed.
Any help is apperciated.
Thank you
  4 commentaires
Sara Boznik
Sara Boznik le 14 Août 2020
It looks like you have only constant.
HN
HN le 14 Août 2020
No, x and y changes over time. but gradient is not because of lost previous information while passing through for loop.
Thanks

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 14 Août 2020
ts=0.0001;
x(t)=cos(2*pi*t) % index of x is t, it cannot be, it shows error
y(t)=sin(2*pi*t)
vx(t)=gradient(x,ts) % index cannot be fraction and to use gradient you need to have x as vector
vy(t)=gradient(y,ts)
S=[vx;vy]
You may rather use:
ts=0.0001 ;
vx = sin(2*pi*t) ;
vy = cos(2*pi*t) ;
S=[vx;vy]
  10 commentaires
KSSV
KSSV le 14 Août 2020
It is suggested to post the code here..so that if not me others also can help you.
HN
HN le 22 Août 2020
Modifié(e) : HN le 22 Août 2020
KSSV ,
Here is the program. I tried to use persistant for t but the dimension exceeded. If t= not a time vector, S become zero all the time.
% time function is to create a time vector
function time(t)
persistent n
n=t;
if isempty(n)
n = 0;
end
n = n+1
end
function [Pose, S] = get_St3PhRS(t)
ts=1/1000;
tt=time(t);
rp=1000; % Radius of the base plate in mm
th=-0.2*cos(2*pi*tt);
psi=0.2*sin(2*pi*tt);
z=707.1068;
phi=atan2(sin(psi)*sin(th),(cos(psi)+cos(th)));
T=Rot('y',th)*Rot('x',psi)*Rot('z',phi)
x=1/2*rp*(-cos(phi)*cos(psi)+cos(phi)*cos(th)+sin(phi)*sin(psi)*sin(th));
y=-rp*cos(psi)*sin(phi);
Pose=[x;y;z;th;psi;phi]
% derivative of moving plate rotation angles
vx=gradient(x,ts);
vy=gradient(y,ts);
vz=gradient(z,ts);
dth=gradient(th,ts); %(2*pi*sin(2*pi*t))/5;
dpsi=gradient(psi,ts); %(2*pi*cos(2*pi*t))/5;
dphi=gradient(phi,ts);%(cos(psi)*sigma1*dpsi+cos(th)*dpsi-cos(psi)*sin(psi)*sin(th)*dth)/(-sigma2*sigma1+sigma2+2*cos(psi)*cos(th)+2*sigma1);
JT=[0, cos(th), cos(psi)*sin(th);1, 0, -sin(psi);0, -sin(th), cos(psi)*cos(th)];
dTh=[dth; dpsi;dphi]
w=JT*dTh;
S=[vx;vy;vz;w(1);w(2);w(3)];
end
function A = InverseVelocityPhRs20200820(T,th,~,q)
:
:
:
S=get_St3PhRS(T) % Here is where the function is called
:
:
A=~~;
end
function [thout,Stm_out,MM_out,Pout,G_out,FRK_OUT,R_out,q_out,Pd_out] = RK4_RhPRS(F,t0,h,tfinal,y0,p0,q0)
% ODE4 Classical Runge-Kutta ODE solver.
th = y0;
R = eye(3);
P = p0;
q = q0;
q_out=q;
thout = th;
Pout = P;
Pd=[-9.9667;0;707.1068];
Pd_out=Pd;
Stm=[0;0;0;1.1080;0; 0.8593];
Stm_out = Stm;
MM_out = [];
G_out = [];
R_out=R;
FRK=[0;0;0;1.1080;0; 0.8593];
FRK_OUT = FRK;
for t = t0 : h : tfinal-h
[k1,~,~,P1,~,~,~,q1] = F(t,th,P,q); % Error in RK4_RhPRS (line 20) : Runge-Kutta
:
:
end
end
% Main function call
[J,S,M,P,G,F,R,q_out,Pd]= RK4_RhPRS(@InverseVelocityPhRs20200820,0,ts,1,th,P,q);
%% Error
Error using time
Too many output arguments.
Error in get_St3PhRS (line 3)
tt=time(t); % t contains all t values but
Error in InverseVelocityPhRs20200820 (line 19)
S=get_St3PhRS(T)
Error in RK4_RhPRS (line 20)
[k1,~,~,P1,~,~,~,q1] = F(t,th,P,q);
Error in Main3PhRS_20200820 (line 103)
[J,S,M,P,G,F,R,q_out,Pd]= RK4_RhPRS(@InverseVelocityPhRs20200820,0,ts,1,th,P,q);

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by