Subscript indices must either be real positive integers or logicals while k starts from 1 .
    5 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
% I've fun_dquat called by  another function RK4 as shown below. Error says, Subscript indices must either be real positive integers or
%logicals. 
function dquat=fun_dquat(k,q)
global wwx 
global wwy
global wwz 
wx=wwx;
wy=wwy;
wz=wwz;
ww=[wx;wy;wz]'; %64 by 3 matrix obtained in each iteration 
Wskew=  [ 0,-ww(k,1),-ww(k,2),-ww(k,3);
ww(k,1),  0, ww(k,3),-ww(k,2);
ww(k,2),-ww(k,3),  0, ww(k,1);
ww(k,3), ww(k,2),-ww(k,1),  0]
coef=0.1;
c=coef*(1-q*q');
dquat=0.5*(Wskew*q')'+c*q;
end
% Function RKF utilize fun_dquat to compute dquat
 function yout = RK4(F,t0,h,tfinal,y0)
   % ODE4  Classical Runge-Kutta ODE solver.
   %   yout = ODE4(F,t0,h,tfinal,y0) uses the classical
   %   Runge-Kutta method with fixed step size h on the interval
   %      t0 <= t <= tfinal
   %   to solve
   %      dy/dt = F(t,y)
   %   with y(t0) = y0.
   %   Copyright 2014 - 2015 The MathWorks, Inc.
      y = y0;
      yout = y;
      for tt = t0 : h : tfinal-h
         s1 = F(tt,y);
         s2 = F(tt+h/2, y+h*s1/2);
         s3 = F(tt+h/2, y+h*s2/2);
         s4 = F(tt+h, y+h*s3);
         y = y + h*(s1 + 2*s2 + 2*s3 + s4)/6;
         yout = [yout; y]; %#ok<AGROW>
      end
      %error message
      RK4 nonunit quaternion integration:
      Subscript indices must either be real positive integers or logicals. 
      Error in fun_dquat (line 9)
      Wskew=  [ 0,-ww(k,1),-ww(k,2),-ww(k,3);    
      Error in RK4 (line 14)
      s1 = F(tt,y);
      Error in PRS20200428 (line 187)
      q_rk4 = RK4(@fun_dquat,0,delt,t_max,q_init); % Integration  to get the next quaternion
    %Can anyone help to figureout where the problem of the code is ?  Thanks
4 Commenti
  Geoff Hayes
      
      
 il 28 Apr 2020
				So the k input to your fun_dquat is neither a positive integer nor logical...and so is invalid as an index into the array. Are these the correct values that you want to pass into this function?
Risposte (1)
  James Tursa
      
      
 il 28 Apr 2020
        The RK4( ) function expects to call the derivative function with the signature F(tt,y), i.e. time is the 1st argument.
But your fun_dquat( ) signature has k as the 1st argument, which you are using as an index in your code.
So you have a mismatch, hence the error.  Your derivative function needs to have the signature that the RK4 expects when making that F(tt,y) and similar calls.
2 Commenti
  James Tursa
      
      
 il 28 Apr 2020
				
      Modificato: James Tursa
      
      
 il 28 Apr 2020
  
			So why do you have a k in your input argument and why are you using k as an index in your code?  I don't see any k in the above formula.  What are the global wwx, wwy, wwz in your code?  Arrays of sampled rates? And you are trying to pick off the correct sample with k?
The RK4 scheme is going to try and generate a derivative at "fractional" times. If all you have is sampled rate arrays at specific times, you will need to interpolate them within the derivative function to get rates at the desired times for the RK4 scheme to use.
Vedere anche
Categorie
				Scopri di più su Downloads in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!