⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simu_neural.c

📁 书籍代码:遗传演算法原理与应用_活用MATLAB(Source Code)
💻 C
字号:
/************ User appended ***********************************
 * File : simu_neural.c
 * Abstract:
 *       An example C-file S-function for Neural control system,
 *       uk = neural(ek,dek)
 *
 * See simulink/src/sfuntmpl_doc.c
 *
 * Copyright 1990-2000 The MathWorks, Inc.
 * $Revision: 1.7 $
 * Application Program Written by Chou, Penchen, July,27,2001
 * EE Dept., Da-Yeh University.
 * Execute mex simu_neural.c to get simu_neural.dll 
 *    before Simulink simulation.
 **************************************************************/


#define S_FUNCTION_NAME  simu_neural
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"

/*================*
 * Build checking *
 *================*/
#if !defined(MATLAB_MEX_FILE)
/*
 * This file cannot be used directly with the Real-Time Workshop. However,
 * this S-function does work with the Real-Time Workshop via
 * the Target Language Compiler technology. See 
 * matlabroot/toolbox/simulink/blocks/tlc_c/timestwo.tlc   for the C version
 * matlabroot/toolbox/simulink/blocks/tlc_ada/timestwo.tlc for the Ada version
 */
# error This_file_can_be_used_only_during_simulation_inside_Simulink
#endif

/**************** User appended ***********************************/
float neural(float ek, float dek);                 
#include <stdio.h>
#include <math.h>
   float Se=1, Sde=1, dSu=1;
   float v11=10.8519, v12=42.0039,
	 v21=-0.3134, v22=-0.3082;
   float b11=-25.3636, b12=-0.0382;
   float w11=0.0188, w12=-2.1422,
	 b21=-0.0650;
   float uk;
   float x1, x2, N11, N21, N12;
/*****************************************************************/   

/* Function: mdlInitializeSizes =====================================
 * Abstract:
 *   Setup sizes of the various vectors.
 */
static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, 0);
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return; /* Parameter mismatch will be reported by Simulink */
    }

    if (!ssSetNumInputPorts(S, 2)) return;         /* 2-input */
    ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
    ssSetInputPortDirectFeedThrough(S, 0, 1);
    ssSetInputPortWidth(S, 1, DYNAMICALLY_SIZED);
    ssSetInputPortDirectFeedThrough(S, 1, 1);

    if (!ssSetNumOutputPorts(S,1)) return;
    ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);

    ssSetNumSampleTimes(S, 1);

    /* Take care when specifying exception free code - see sfuntmpl_doc.c */
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE |
                 SS_OPTION_USE_TLC_WITH_ACCELERATOR);
}


/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    Specifiy that we inherit our sample time from the driving block.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}


/* Function: mdlOutputs =======================================================
 * Abstract:
 *    y = neural(ek,dek)
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{
    int_T             i;
    InputRealPtrsType uPtrs1 = ssGetInputPortRealSignalPtrs(S,0); /* input #1 */
    InputRealPtrsType uPtrs2 = ssGetInputPortRealSignalPtrs(S,1); /* input #2 */
    real_T            *y    = ssGetOutputPortRealSignal(S,0);
    int_T             width = ssGetOutputPortWidth(S,0);

    for (i=0; i<width; i++) {
        /*
         * This example does not implement complex signal handling.
         * To find out see an example about how to handle complex signal in 
         * S-function, see sdotproduct.c for details.
         */
        *y++ = neural(*uPtrs1[i], *uPtrs2[i]); 
        
    }
}


/* Function: mdlTerminate =====================================================
 * Abstract:
 *    No termination needed, but we are required to have this routine.
 */
static void mdlTerminate(SimStruct *S)
{
}

#if defined(MATLAB_MEX_FILE)
#define MDL_RTW
/* Function: mdlRTW ===========================================================
 * Abstract:
 *	Since we've declared all are parameters as non-tunable, we need
 *	only provide this routine so that they aren't written to the model.rtw
 *	file. The values of the parameters are implicitly encoded in the
 *	sample times.
 */
static void mdlRTW(SimStruct *S)
{
}
#endif /* MDL_RTW */

#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
#include "cg_sfun.h"       /* Code generation registration function */
#endif

/***************** User appended *********************************/
float neural(float ek, float dek)
{
   x1=Se*ek;
   x2=Sde*dek;
   N11=v11*x1+v12*x2+b11;
   //printf("N11=%f\n",N11);
   N11=2.0/(1+exp(-2*N11))-1.0;
   //printf("N11=%f\n",N11);
   N12=v21*x1+v22*x2+b12;
   N12=2.0/(1+exp(-2*N12))-1.0;
   N21=w11*N11+w12*N12+b21;
   uk=dSu*N21;
   return uk;
}   
/******************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -