cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

How to create custom function in Mathcad

DM_10631844
5-Regular Member

How to create custom function in Mathcad

 
 
 
 

Greetings, I tried to create custom functions in mathcad but I was able to do it, however I wanted to create that function fc and fc1 and I couldn't, I get an error message I understand how to compile and tried other functions but when I go to do it with results of vectors and matrices I have this problem here I attach the code in C only for the fc function, could you help me how to create both custom functions please?




 

 

6 REPLIES 6
DM_10631844
5-Regular Member
(To:DM_10631844)

1111DM_10631844_0-1721038678159.png

 

 

Use subscript rather than function notation when asking for components:

 

More like:

fc.png

DM_10631844
5-Regular Member
(To:AlanStevens)

111111

11

 

 

DM_10631844
5-Regular Member
(To:DM_10631844)

#include "mcadincl.h"

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

// table of error messages
char * myErrorMessageTable[NUMBER_OF_ERRORS] =
{ "interrupted",
"insufficient memory",
"must be real"
};

// this code executes the operation
LRESULT CreateVectorWithXSquare(COMPLEXARRAY * const Vector, LPCCOMPLEXSCALAR Scalar)
{
unsigned int i;

// check that the scalar argument is real
if (Scalar->imag != 0.0)
return MAKELRESULT(MUST_BE_REAL, 1);

// allocate memory for the vector
if (!MathcadArrayAllocate(Vector, 4, 1, TRUE, FALSE))
return INSUFFICIENT_MEMORY;

// fill the vector with the square of the scalar
for (i = 0; i < 4; i++) {
if (isUserInterrupted()) {
MathcadArrayFree(Vector);
return INTERRUPTED;
}
Vector->hReal[i][0] = Scalar->real * Scalar->real;
}

// normal return
return 0;
}

// fill out a FUNCTIONINFO structure with the information needed for registering the function with Mathcad
FUNCTIONINFO fc2 =
{
"fc2",
"x",
"returns a vector of length 4 with the square of the scalar x",
(LPCFUNCTION)CreateVectorWithXSquare,
COMPLEX_ARRAY,
1,
{ COMPLEX_SCALAR }
};

// DLL entry point code
BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved);

BOOL WINAPI DllEntryPoint(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
if (CreateUserErrorMessageTable(hDLL, NUMBER_OF_ERRORS, myErrorMessageTable))
CreateUserFunction(hDLL, &fc2);
break;

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
break;
}
return TRUE;
}

#undef INTERRUPTED
#undef INSUFFICIENT_MEMORY
#undef MUST_BE_REAL
#undef NUMBER_OF_ERRORS

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

 

DM_10631844
5-Regular Member
(To:DM_10631844)

#include "mcadincl.h"

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

// table of error messages
char * myErrorMessageTable[NUMBER_OF_ERRORS] =
{ "interrupted",
"insufficient memory",
"must be real"
};

// this code executes the operation
LRESULT CreateRowMatrixWithXSquare(COMPLEXARRAY * const Matrix, LPCCOMPLEXSCALAR Scalar)
{
unsigned int i;

// check that the scalar argument is real
if (Scalar->imag != 0.0)
return MAKELRESULT(MUST_BE_REAL, 1);

// allocate memory for the matrix
if (!MathcadArrayAllocate(Matrix, 1, 4, TRUE, FALSE))
return INSUFFICIENT_MEMORY;

// fill the row with the square of the scalar
for (i = 0; i < 4; i++) {
if (isUserInterrupted()) {
MathcadArrayFree(Matrix);
return INTERRUPTED;
}
Matrix->hReal[0][i] = Scalar->real * Scalar->real;
}

// normal return
return 0;
}

// fill out a FUNCTIONINFO structure with the information needed for registering the function with Mathcad
FUNCTIONINFO fc1 =
{
"fc1",
"x",
"returns a 1x4 row matrix with the square of the scalar x",
(LPCFUNCTION)CreateRowMatrixWithXSquare,
COMPLEX_ARRAY,
1,
{ COMPLEX_SCALAR }
};

// DLL entry point code
BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved);

BOOL WINAPI DllEntryPoint(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
if (CreateUserErrorMessageTable(hDLL, NUMBER_OF_ERRORS, myErrorMessageTable))
CreateUserFunction(hDLL, &fc1);
break;

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
break;
}
return TRUE;
}

#undef INTERRUPTED
#undef INSUFFICIENT_MEMORY
#undef MUST_BE_REAL
#undef NUMBER_OF_ERRORS

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

 

DM_10631844
5-Regular Member
(To:DM_10631844)

 

The first code is fc2, it doesn't work, it gives an error and the 2nd is fc1, it works fine in C++

DM_10631844_0-1721041023864.png

 

 

 

 

 

Top Tags