0% found this document useful (0 votes)
15 views

Pointer To Input Port0 /: #Define #Define #Include #Include #Define

This document contains code for a Simulink S-function that defines a discrete-time system with 3 states. It initializes the sample time to 0.001 seconds, initializes the states to 0, defines the output as the state values, and updates the state values based on input signals at each time step. The state values are sinusoidal functions of the input signals and time.

Uploaded by

yusrisetiawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Pointer To Input Port0 /: #Define #Define #Include #Include #Define

This document contains code for a Simulink S-function that defines a discrete-time system with 3 states. It initializes the sample time to 0.001 seconds, initializes the states to 0, defines the output as the state values, and updates the state values based on input signals at each time step. The state values are sinusoidal functions of the input signals and time.

Uploaded by

yusrisetiawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#define S_FUNCTION_LEVEL 2

#define S_FUNCTION_NAME sourcediskrit


#include "simstruc.h"
#include <math.h>
#define U(element) (*uPtrs[element]) /*Pointer to Input Port0*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumDiscStates(S, 3);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0,2);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortOverWritable(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 3);
ssSetNumSampleTimes(S, 1);
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE
| SS_OPTION_DISCRETE_VALUED_OUTPUT));
}
static void mdlInitializeSampleTimes(SimStruct *S){
ssSetSampleTime(S, 0, 0.001);
ssSetOffsetTime(S, 0, 0.0);}
#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S){
real_T *x = ssGetRealDiscStates(S);
int_T nXStates = ssGetNumDiscStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);

/* initialize the states to 0.0 */


x[0] = 0;
x[1] = 0;
x[2] = 0;
x[3] = 0;
}
static void mdlOutputs(SimStruct *S, int_T tid){
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetRealDiscStates(S);
y[0] = x[0];
y[1] = x[1];
y[2] = x[2];
}
#define MDL_UPDATE
static void mdlUpdate(SimStruct *S, int_T tid)
{

real_T *x = ssGetRealDiscStates(S);
real_T t = ssGetT(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T a,f ;
f=U(0);
a=U(1);
x[0]= a*sin(2*3.14*f*t);
x[1]= a*sin((2*3.14*f*t)-(2*3.14/3));
x[2]= a*sin((2*3.14*f*t)+(2*3.14/3));
}
static void mdlTerminate(SimStruct *S)
{ } /*Keep this function empty since no memory is allocated*/
#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

You might also like