Using matrices to create 3D rendering

Hello,
I am currently working on a project for my Multivariable calculus class, where we have to turn a set of data into a 3D model.
I currently have mapped out the US and put in the data points for each state, then turned that into a matrix. However, once the matrix is in my code I get the error messages
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in practice (line 36)
zc = interp2(x,y,z,xi,yi,'cubic');
This is the code I currently have
x=0:50;
y=0:50;
z = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 24609 24609 24609 24609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 24609 24609 24609 24609 24609 27130 31065 31065 31065 31065 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31925 31295 0 0
;0 0 0 24609 24609 24609 24609 24609 27130 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31295 31295 31295 0
;0 0 0 27321 27321 27321 27321 27321 27321 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 31915 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 31295 31295 31295 0
;0 0 27321 27321 27321 27321 27321 27130 27130 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 28662 36367 31295 0 0
;0 0 27321 27321 27321 27321 27321 27130 27130 27130 31065 31065 31065 31065 31065 31065 31362 31362 31362 31362 31362 31915 31915 31915 30059 30059 30059 0 0 27532 27532 0 0 0 0 0 30346 30346 28662 36367 0 0 0
;0 0 22744 27321 27321 27321 27321 27130 27130 27130 27130 25378 25378 25378 25378 25378 31362 31362 31362 31362 31362 31915 31915 31915 30059 30059 30059 30059 0 27532 27532 0 0 0 30346 30346 30346 30346 31563 31563 0 0 0
;0 22744 22744 22744 24128 24128 24128 27130 27130 27130 27130 25378 25378 25378 25378 25378 31362 31362 31362 31362 31362 31915 31915 31915 31915 30059 30059 30059 0 27532 27532 27532 0 30346 30346 30346 30346 30346 31217 35494 0 0 0
;0 22744 22744 22744 24128 24128 24128 24128 19975 19975 19975 25378 25378 25378 25378 25378 26585 26585 26585 26585 26585 29801 29801 29801 29801 29801 30059 30059 0 27532 27532 27532 30351 35759 35759 35759 35759 29878 0 0 0 0 0
;0 22744 22744 22744 24128 24128 24128 24128 19975 19975 19975 25378 25378 25378 25378 25378 26585 26585 26585 26585 26585 26585 29801 29801 29801 29801 29271 29271 29562 29562 30351 30351 30351 35759 35759 35759 35759 29878 0 0 0 0 0
;0 0 22744 22744 24128 24128 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 26585 26585 26585 26585 26585 29801 29801 29801 29271 29271 29271 29562 29562 30351 30351 30351 27708 27455 27455 27455 33838 0 0 0 0 0
;0 0 22744 22744 22744 24128 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 28776 28776 28776 28776 28776 28776 27532 27532 27532 29271 29271 29562 29562 30351 30351 27708 27708 27708 29296 27455 0 0 0 0 0 0
;0 0 22744 22744 22744 22744 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 28776 28776 28776 28776 28776 28776 27532 27532 27532 29271 29271 29562 29562 28910 28910 27708 27708 29296 29296 29296 29296 0 0 0 0 0
;0 0 0 22744 22744 22744 24128 23447 23447 23447 23447 21373 21373 21373 21373 21373 21373 28776 28776 28776 28776 28776 28776 27532 27532 27532 27532 29271 28910 28910 28910 28910 28910 25562 25562 25562 25562 25562 0 0 0 0 0
;0 0 0 0 22744 22744 22744 23447 23447 23447 23447 21373 21373 21373 21373 21373 25856 25856 25856 25856 25856 25856 25856 27532 27532 27532 27532 26981 26981 26981 26981 26981 26981 25562 25562 25562 25562 25562 0 0 0 0 0
;0 0 0 0 22744 22744 22744 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 25856 25856 25856 25856 26859 26859 26859 26859 26981 26981 26981 26981 27657 30123 30123 30123 30123 25562 0 0 0 0 0 0
;0 0 0 0 22744 22744 23447 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 25856 25856 25856 25856 26859 26859 26859 29384 29384 31275 31275 31275 27657 27657 30123 30123 30123 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 26292 26292 26292 26292 26292 26859 26859 29384 29384 31275 31275 31275 27657 27657 27657 30123 30123 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 29384 29384 31275 31275 31275 27657 27657 27657 27657 0 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 29384 29384 31275 31275 31275 27657 27657 27657 24461 0 0 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 27138 27138 31275 24461 24461 24461 24461 24461 24461 0 0 0 0 0 0 0 0
;0 0 0 26008 26008 26008 26008 26008 26008 0 0 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 27138 27138 27138 27138 0 0 0 0 0 24461 24461 24461 0 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 26008 0 0 26092 0 26092 0 0 0 0 26292 26292 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0 0
;0 0 0 26008 26008 26008 26008 26008 26008 0 0 0 0 0 26092 26092 0 0 0 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 26008 0 0 0 0 0 26902 26092 0 0 0 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0
;0 0 0 0 26008 0 0 0 0 26008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 0 26008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
];
xi=0:0.5:50;
yi=0:0.5:50;
[xi,yi] = meshgrid(xi,yi);
zc = interp2(x,y,z,xi,yi,'cubic');
mesh(xi,yi,zc)
I have just started learning MATLAB this year, so all the help I can get would be appreciated.
Thank you

 Accepted Answer

Your z is 29 x 43. When you call interp2(x, y, z, ...) then length(x) must match size(z,2) and length(y) must match size(z,1) so length(x) must be 43 and length(y) must be 29. But you define both x and y as 0:50 which is length(51).
If you have 29 y values that are intended to span from 0 to 50, then use linspace(0, 50, 29) to create the coordinates.

2 Comments

Hello,
I have changed my code as to what you have reccomended,
x=0:42;
y=0:28;
and
xi=0:0.1:42;
yi=0:0.1:28;
which allows the program to run, so thank you!
Also had to reverse the data, as it came out as a mirrored version from my original program.
The simmulation also seems to show some negative values on the graph (despite not putting negative values in the matrix), would there be a way to fix that or is that just natural?
(I inserted a photo of the graph below)
practice graph 2.png
You have some steep edges. cubic interpolation near them results in some values near -4866, and spline interpolation results in some values near -11827, and makima interpolation results in some values near -2682. None of those interpolation methods expect sudden flatness: they all expect local curvature.
It works the other way too: those interpolations also produce overshoot on the peaks.
You can use 'linear' as the interpolation and it will not give any negative values.... but the result will also look pretty blocky.
You might want to try
zt = z;
zt(zt == 0) = nan;
zc = interp2(x, y, z, xi, yi, 'cubic');
zc(isnan(zc)) = 0;
mesh(xi, yi, zc)
This would treat the non-zero region as being separate from the region of 0. You would not get any edge interpolation down to zero, and so no undershoot to negatives.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!