Mexfunction: Undefined function or variable
7 views (last 30 days)
Show older comments
Hello,
I am trying to use Mexfunction in the matlab but I got a problem.
#include "mex.h"
#include "blas.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/* The computational routine */
void findvalue(double *Vnew, double *V, double *Vp, double *Ptrans, double *U, double *C, double *Y, double *A, double R, double sig, double beta, double it, double itmax, double tol, double dif, int Na, int Ny, double *Z)
{
while (dif >= tol && it <= itmax) {
it = it + 1;
for (int i = 0; i < Na; i++) {
for (int j = 0; j < Ny; j++) {
/*V[i][j] = Vnew[i][j];*/
V[j+i*Ny] = Vnew[j+i*Ny];
}
}
for (int a=0; a<Na; a++) {
for (int b=0; b<Ny; b++){
for (int c=0; c<Na; c++){
C[c] = Y[b] + A[a] - A[c]/R;
if (C[c] >= 0) {
U[c] = 1 / (1 - 1 / sig) * pow(C[c], 1 - 1 / sig);
}
else {
U[c] = -9999999999999 -1;
}
for (int d = 0; d < Ny; d++) {
/*Vp[c] = U[c] + beta * (V[c][d] * Ptrans[d] + V[c][d] * Ptrans[d] + V[c][d] * Ptrans[d]);*/
Vp[c] = U[c] + beta * (V[d+c*Ny] * Ptrans[d] + V[d+c*Ny] * Ptrans[d] + V[d+c*Ny] * Ptrans[d]);
}
/*Vnew[a][b] = Vp[0];*/
Vnew[b+a*Ny] = Vp[0];
for (int i = 1; i < Na; i++) {
if /*(Vp[i] > Vnew[a][b])*/(Vp[i] > Vnew[b+a*Ny]){
/* Vnew[a][b] = Vp[i];*/
Vnew[b+a*Ny] = Vp[i];
}
else {
/*Vnew[a][b] = Vnew[a][b];*/
Vnew[b+a*Ny] = Vnew[b+a*Ny];
}
}
}
}
}
double tempdiff[Na*Ny];
for (int i = 0; i < Na; i++) {
for (int j = 0; j < Ny; j++) {
/*tempdiff[i][j] = abs(V[i][j] - Vnew[i][j]);*/
tempdiff[j+i*Ny] = abs(V[j+i*Ny] - Vnew[j+i*Ny]);
}
}
dif = tempdiff[0];
for (int i = 0; i < Na; i++) {
for (int j = 0; j < Ny; j++) {
if (tempdiff[j+i*Ny] > dif) {
dif = tempdiff[j+i*Ny];
}
else {
dif = dif;
}
}
}
}
for (int i = 0; i < Na; i++) {
for (int j = 0; j < Ny; j++) {
Z[j+i*Ny] = Vnew[j+i*Ny];
}
}
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *outMatrix; /* output matrix */
double *Vnewmatrix;
double *Vmatrix;
double *Z;
double *Vpmatrix;
double *Ptransmatrix;
double *Umatrix;
double *Cmatrix;
double *Ymatrix;
double *Amatrix;
double Rscalar;
double sigscalar;
double betascalar;
double itscalar;
double itmaxscalar;
double tolscalar;
double difscalar;
int Nascalar;
int Nyscalar;
/* get the value of the scalar input */
Vnewmatrix = mxGetPr(prhs[0]);
Vmatrix = mxGetPr(prhs[1]);
Vpmatrix = mxGetPr(prhs[2]);
Ptransmatrix = mxGetPr(prhs[3]);
Umatrix = mxGetPr(prhs[4]);
Cmatrix = mxGetPr(prhs[5]);
Ymatrix = mxGetPr(prhs[6]);
Amatrix = mxGetPr(prhs[7]);
Rscalar = mxGetScalar(prhs[8]);
sigscalar = mxGetScalar(prhs[9]);
betascalar = mxGetScalar(prhs[10]);
itscalar = mxGetScalar(prhs[11]);
itmaxscalar = mxGetScalar(prhs[12]);
tolscalar = mxGetScalar(prhs[13]);
difscalar = mxGetScalar(prhs[14]);
Nascalar = mxGetScalar(prhs[15]);
Nyscalar = mxGetScalar(prhs[16]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(Nascalar, Nyscalar ,mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
findvalue(Vnewmatrix, Vmatrix, Vpmatrix, Ptransmatrix, Umatrix, Cmatrix, Ymatrix, Amatrix, Rscalar, sigscalar, betascalar, itscalar, itmaxscalar, tolscalar, difscalar, Nascalar, Nyscalar, outMatrix);
}
And I saved this as optsavlooptest2.c in the folder
Then I made a new code
clear all;
clc;
tic
cd 'C:\Users\chang\Desktop\New Folder'
mex optsavlooptest2.c
% ================ 1. Parameters and Constants ============================
%Iteration Parameters
tol = 0.0001;
itmax = 1000;
%Model Parameters and utility function
sig = 0.75; %intertemporal elasticity of substitution
beta = 0.95; %discount factor
R = 1/beta - 0.00215;
Utility = @(X) (1/(1-1/sig)*X^(1-1/sig));
% =============== 2. Discretizing the state space =========================
%Variables for discretization of state space
Amin = -20;
Amax = 60;
Na = 4*80;
Ymin = 2;
Ymax = 6;
Ny = 3;
%Discretization of state space
A = linspace(Amin, Amax, Na);
Y = linspace(Ymin, Ymax, Ny);
% === 3. Initial guesses, Variable initialization and Transition matrix ===
%Initial guess for value function
V = ones(Na,Ny);
%Initialization of other variables
C = ones(Na,1);
U = ones(Na,1);
Ptrans = [1/3 1/3 1/3]';
Vnew = ones(Na,Ny);
Vp = ones(Na,1);
%Value function iteration
it = 0;
dif = 1;
% ================ 4. Value function iteration ============================
AAA = findvalue(Vnew, V, Vp, Ptrans, U, C, Y, A, R, sig, beta, it, itmax, tol, dif, Na, Ny);
Then when I run the last line, I got en error code: Undefined function or variable 'findvalue'
I think I already made a function findvalue in optsavlooptest2.c file so I have no idea why this problem happens.
Any solutions for this situation?
Thanks in advance.
0 Comments
Accepted Answer
James Tursa
on 12 Feb 2021
Edited: James Tursa
on 13 Feb 2021
Your m-code can't see C functions inside your mex routine. You need to call the mex routine by its filename, and then inside the mex routine your findvalue C function gets called by your C code. E.g., the m-code is simply
AAA = optsavlooptest2(Vnew, V, Vp, Ptrans, U, C, Y, A, R, sig, beta, it, itmax, tol, dif, Na, Ny);
Also, note that you don't need to mex the file each time you run your script. Typically you would mex the file once from the command line, and then in your script simply call the optsavlooptest2 function.
0 Comments
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!