Reputation: 11
I am trying to use matlab for calculating the approximation of a function using the composite trapezoidal rule, and then displaying the function and approximation using a surf function and a bar3 function. the thing is is that when I try plot the function surf(x,y,Z) I receive and error saying dimension's mismatch.
my question is how would I get the surf function to plot the 3D graph when my x,y and z arrays differ in size.
I've tried to create zeros functions of the the x and y array's of the same size and then adding my values to each, then NaN'ing the extra 0's, but as u see each of my arrays start with 0's therefore NaN'ing where i find a zero in my arrays will effect my graph plot. and plus i still get the same error "dimensions mismatch" so i supposed thats because my Z array is bigger than my x and y.
Any help would be appreciated.
code for my x and y are: `
x = linspace(a,b,h); %h being 11 and breaks up the difference because datapoints a and b into h number of sub intervals
y = linspace(c,d,k); %k being 6 and breaks up the difference because data points c and d into k number of sub intervals
Z = zeros(h,k);
for i = 1:1:h
for j = 1:1:k
Z(i,j) = f(x(i),y(j));
end
end
surf(x,y,Z); `
x
0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3.0000
y
0 0.6286 1.2571 1.8857 2.5143 3.1429
Z
0 0 0 0 0 0
0 0.1764 0.2854 0.2852 0.1761 -0.0004
0 0.3528 0.5707 0.5705 0.3522 -0.0008
0 0.5292 0.8561 0.8557 0.5283 -0.0011
0 0.7056 1.1415 1.1410 0.7044 -0.0015
0 0.8820 1.4268 1.4262 0.8804 -0.0019
0 1.0584 1.7122 1.7115 1.0565 -0.0023
0 1.2348 1.9975 1.9967 1.2326 -0.0027
0 1.4112 2.2829 2.2820 1.4087 -0.0030
0 1.5876 2.5683 2.5672 1.5848 -0.0034
0 1.7640 2.8536 2.8525 1.7609 -0.0038
Error using surf (line 75) Data dimensions must agree.
Error in CompositeTrapazoidal>btnSolve_Callback (line 167) surf(x,y,Z);
Upvotes: 1
Views: 1231
Reputation:
Try surf(x,y,Z');
(because x
's length should match the Z
's column count)
Upvotes: 1