Skip to main content
12-Amethyst
July 13, 2024
Question

How to create custom function in Mathcad

  • July 13, 2024
  • 1 reply
  • 1959 views
 
 
 
 

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?




 

 

1 reply

12-Amethyst
July 15, 2024

1111DM_10631844_0-1721038678159.png

 

 

19-Tanzanite
July 15, 2024

Use subscript rather than function notation when asking for components:

 

More like:

fc.png

12-Amethyst
July 15, 2024

#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