Error: Grid values must be strictly increasing- interp2 function MATLAB Coder
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi All,
This is my sample snippent. When I try to convert to C code using Matlab coder I am getting Index exceeds dimensions and Grid Values must be strictly increasing. However Dimensions error is fixed, can see here. Question seems big though, because I hard coded inputs for interp2 function. These two erros are haunting me from past week. I would appreciate if someone could shed light on this.
%codegen
function test()
CellModel=struct('SOCgrid',zeros(5,10),'Tgrid',zeros(5,10),'R0',zeros(5,10));
coder.varsize('SimCoreIn.R0_cell');
SimCoreIn=struct('R0_cell',zeros(102,3),'CellModel',CellModel,'SOC_cell',zeros(102,1),'T_cell',zeros(102,1));
SimCoreIn.CellModel.SOCgrid= [
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710]; %size 5 x 10
SimCoreIn.CellModel.Tgrid=[ 20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
40 40 40 40 40 40 40 40 40 40
50 50 50 50 50 50 50 50 50 50
60 60 60 60 60 60 60 60 60 60]; %size 5 x 10
SimCoreIn.CellModel.R0 = [0.0120 0.0120 0.0130 0.0135 0.0135 0.0146 0.0147 0.0150 0.0160 0.0191
0.0120 0.0120 0.0129 0.0129 0.0129 0.0128 0.0130 0.0131 0.0136 0.0150
0.0125 0.0120 0.0119 0.0120 0.0119 0.0119 0.0120 0.0111 0.0114 0.0113
0.0116 0.0116 0.0116 0.0115 0.0115 0.0115 0.0115 0.0116 0.0129 0.0121
0.0112 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0124 0.0121];
%size 5 x 10
SimCoreIn.SOC_cell=[
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1]; %size 102 x 3
SimCoreIn.T_cell=[20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20]; %size 102 x 3
SimCoreIn.R0_cell=interp2(SimCoreIn.CellModel.SOCgrid,SimCoreIn.CellModel.Tgrid,SimCoreIn.CellModel.R0,SimCoreIn.SOC_cell,SimCoreIn.T_cell,'spline');
Here is the error image.
Thank You
댓글 수: 2
Mukund Sankaran
2021년 4월 8일
The documentation for interp2 (https://www.mathworks.com/help/matlab/ref/interp2.html) has a section on C/C++ code generation at the bottom which mentions this:
For best results, provide X and Y as vectors. The values in these vectors must be strictly monotonic and increasing
The X and Y in your example i.e. SimCoreIn.CellModel.SOCgrid and SimCoreIn.CellModel.Tgrid likely do not meet this criteria which is why you are seeing the error.
채택된 답변
Nathan S
2021년 4월 9일
As Mukund said, the issue is that your grid points must be monotonic and increasing. In your case, SOCgrid is decreasing. I am not sure why this is working in MATLAB, as according to the doc they also require the inputs to be increasing. For codegen, requiring the inputs to be in the right order provides a significant performance increase, which is especially important for this function.
In your case, the fix for this is very straightforward. All you need to do is change the order that you specify SOCgrid. This also means that you must adjust the order of the values in R0. I have provided the fixed version below, which works in codegen for me:
SOCgridPoints= [-0.0710 0.0360 0.1430 0.2510 0.3580 0.4650 0.5720 0.6790 0.7860 0.8930];
TgridPoints=20:10:60;
[SimCoreIn.CellModel.SOCgrid,SimCoreIn.CellModel.Tgrid] = meshgrid(SOCgridPoints, TgridPoints);
SimCoreIn.CellModel.R0 = [ 0.0191 0.0160 0.0150 0.0147 0.0146 0.0135 0.0135 0.0130 0.0120 0.0120;...
0.0150 0.0136 0.0131 0.0130 0.0128 0.0129 0.0129 0.0129 0.0120 0.0120;...
0.0113 0.0114 0.0111 0.0120 0.0119 0.0119 0.0120 0.0119 0.0120 0.0125;...
0.0121 0.0129 0.0116 0.0115 0.0115 0.0115 0.0115 0.0116 0.0116 0.0116;...
0.0121 0.0124 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0112];
I recommend using meshgrid, as it makes making these kinds of adjustments easier and cuts down on the repitition a little.
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!