Just trying to understand How Matlab define the faces of a surface after given a set of points.
A = [2.6000 3.4000;
2.6000 3.4000;
2.6000 3.4000;
2.6000 3.4000;
2.6000 3.4000];
B=[0.6000 0.6000;
0.6000 0.6000;
1.4000 1.4000;
1.4000 1.4000;
0.6000 0.6000]
C=[ 0 0;
1 1;
1 1;
0 0;
0 0]
surface(A, B, C, C,'Marker','*')
view(3)
As I can see that the command surface() creates only 4 faces, Why two faces are not recognised as all 8 coordinates were given?? What is the mechanism that MATLAB uses two define a face from a set of points.
I have read some documentation , but the only one that describes faces are the one related to patch() function, but following this instructions then there would have been shown only 2 faces as A(1:,1) and B(1:,1) and C(1:,1) creates ONE FACE and the OTHER FACE by A(1:,2) and B(1:,2) and C(1:,2) but ACTUALLY those are the TWO FACES that are not shown by using surface() command, anyone who can shed light on this topic please?
Related
I have a patch with x,y,c coordinates as given below. Upon changing the order of points, MATLAB's patch color interpolation changes.
x = [0.9000 1.0000 1.0000 0.9000]';
y = [0.5000 0.5000 0.6000 0.6000]';
c = [0.0278 0.0371 0.0325 0.0282]';
figure();
patch(x,y,c);
order = [2:4,1];
figure();
patch(x(order),y(order),c(order));
The above produces two different color patches. However, the coordinates and the color have only changed the sequence in cyclic manner.
Any suggestions to overcome this?
I think that this is because you're not closing your patch -- if the last point in your patch is not the same as the first point, MATLAB automatically closes it. Apparently this does something strange with the color interpolation. If you modify the code slightly so that your patch is closed, like so:
figure;
order = [1:4,1];
patch(x(order),y(order),c(order));
figure;
order = [2:4,1:2];
patch(x(order),y(order),c(order));
figure;
order = [3:4,1:3];
patch(x(order),y(order),c(order));
Then you get the same patch every time.
Let's say f(x) = (4x^2-9)/(2x-3). The function f(x) is undefined at x=3/2. Note that the function can be factored to yield f(x)=2x+3 but let's examine the first equation. In the following script when x=1.5, the function f(x=1.5)=4.0
clear all
clc
x = 0:0.3:2;
for i = 1:length(x)
if x(i) == 1.5 % this line for some reasons is ignored.
y(i) = 6;
else
y(i) = (4*x(i)^2-9)/(2*x(i)-3);
end
end
x
y
The output of the preceding script is
x = 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000
y = 3.0000 3.6000 4.2000 4.8000 5.4000 4.0000 6.6000
Why y=4.0000 when x=1.5000? Now let's run the code without for-loop,
clear all
clc
x = 0:0.3:2
y = (4*x.^2-9)/(2*x-3)
The result of the above code is
x = 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000
y = 3.6000
There is only one value for f(x). Can any one explain what is going on?
As for your first question, yes, you are running into a floating point precision error. You can check this by checking the difference between the x value that it's supposed to be a 1.5 and a 1.5.
x(6)-1.5
%ans=
% -2.2204e-16
Specifically in your case it comes from using 0.3 to construct the vector x since that value cannot be precisely saved into binary, see here for a deeper explanation
Any of the following should solve your problem
x=0:3:20; %Create the vector based on values that can be represented
x=x/10;
x=[0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]; %Directly input the values
abs(x(i)-1.5) < tol %Instead of directly comparing values, compare the difference to a determined tolerance (very small compared to the values at hand)
As for your second question #Phill already gave you the answer, you are using / matrix division, and you want ./ element wise division.
When I run your first example with the for loop in Octave, I do not see a problem with the x=1.5 if statement being ignored. Perhaps this is a subtle difference between Matlab and Octave for this although I would be surprised.
For the array notation second example
clear all
clc
x = 0:0.3:2
y = (4*x.^2-9)/(2*x-3)
You have chosen the matrix division operator / instead of the element by element division operator ./
I have two sets of data. Each of them is a matrix and they have two columns, first column represents index x and second represents y. I want to see how similar are the curves of these datasets. In other words I need to have the correlation of these two curves represented by two matrices.
Thanks for help.
You might be looking for the corr2 function which calculates the correlation coefficient for each corresponding values in your matrices:
CorrMatrix = corr2(A,B)
plot(x,CorrMatrix);
Is that what you meant? If not please don't hesitate to ask for more details.
For matrices of unequal size I think you only have these options:
If you have the Signal Processing Toolbox you can use the cross-correlation function xcorr2, otherwise you can do the following:
1)either you calculate the correlation of you data where you use a part of the larger matrix so that the number of elements are similar: (the values I use are really dummy values sorry.)
clear
clc
% Create dummy matrices of unequal sizes
x =1:10;
x2 = 1:6;
A(:,1) = x;
A(:,2) = sin(x);
B(:,1) = x2;
B(:,2) = cos(x2);
A,B
C = corr2(A(1:6,:),B)
A =
1.0000 0.8415
2.0000 0.9093
3.0000 0.1411
4.0000 -0.7568
5.0000 -0.9589
6.0000 -0.2794
7.0000 0.6570
8.0000 0.9894
9.0000 0.4121
10.0000 -0.5440
B =
1.0000 0.5403
2.0000 -0.4161
3.0000 -0.9900
4.0000 -0.6536
5.0000 0.2837
6.0000 0.9602
C =
0.9463
or 2)
After some googling I saw that a similar question was posted here, in which it said that you can play around with Fourier transforms to get the correlation:
Cross-correlation in matlab without using the inbuilt function?
Ok guys. I have the following problem:
I have the data of the following plot.
So the data file of this plot contains three columns.
The 2nd and 3rd ones the x,y points. And the 1st one is to which system those points belong.
In this case the red ones are for the system of 20 years. The blue ones for the 30 years.
What I want to find is the curve at 25 years. So if I plot it it should be between the red and blue curves.
I have no idea how interpolate the data in order to obtain what I want. Actually I want to have for 21,22,...29 years, I guess if we can find it for a time in between these two, then the method should work for any time between 20 and 30.
PS: I guess the interpolation for each curve (in this case red or blue one) is quite easy. Just using interp1(x,y,xx) will work. But what happened with the other "dimension" (M)
The data.
20.0000 3.4076 0
20.0000 3.4226 99.5405
20.0000 3.4701 196.3360
20.0000 3.5592 287.0781
20.0000 3.6248 328.8516
20.0000 3.6643 348.3373
20.0000 3.7091 367.2823
20.0000 3.7591 385.4784
20.0000 3.8077 402.7170
20.0000 3.8957 437.5221
20.0000 4.0314 506.9907
30.0000 3.6335 0
30.0000 3.6373 49.8884
30.0000 3.6488 99.5405
30.0000 3.6685 148.5936
30.0000 3.7363 243.2204
30.0000 3.7876 287.7398
30.0000 3.8537 329.6097
30.0000 3.8935 349.9452
30.0000 3.9384 368.9776
30.0000 3.9892 387.2576
30.0000 4.0410 404.5759
30.0000 4.1350 439.5416
30.0000 4.2153 474.2420
30.0000 4.2813 509.3309
Actually, by looking at the Matlab documentation I found a simpler way. You can use the function griddata. (The doc in matlab help shows visual example). The resampling on a common grid and the interpolation is embedded in the function.
%// First separate (and name your column to identify them better)
t = d(:,1) ;
x = d(:,2) ;
y = d(:,3) ;
%// use the function 'griddata'
[TI,YI] = meshgrid( 20:30 , 0:20:500 ) ; %// change these values to change the grid limits
XI = griddata(t,y,x,TI,YI) ;
%// show result in 3D ... but could be projected in X-Y plane if necessary
plot3(TI,YI,XI , 'Marker','o' )
xlabel('Time') ; ylabel('Y') ; zlabel('X')
The last line of the code shows this plot:
All your interpolated data are in the XI matrix. The way to retrieve them depends on how you want to organize them ultimately.
EDIT:
To place all the interpolated data in a single table InterpData organized the same way of your original table, use the following:
nLine = numel(XI) ;
InterpData = [ reshape(TI,nLine,[]) reshape(XI,nLine,[]) reshape(YI,nLine,[]) ] ;
Regarding the NaNs. They will come to bother you every time you ask to do an interpolation outside of the initially known values.
For example, if your time in the original data is in the [20 to 30] interval, matlab will gladly interpolate anything within that interval, but will return NaN if you ask to return a value for time = 19 for example. Same goes for Y, the grid on which to interpolate has to be within the initial range. (as in this implementation we use a base grid formed by Time (column 1) and Y(column 3), to interpolate the X column).
Try this code, which implements #Hoki 's comment:
m20=[3.4076 0; 3.4226 99.5405; 3.4701 196.3360; 3.5592 287.0781; 3.6248 328.8516; 3.6643 348.3373; 3.7091 367.2823; 3.7591 385.4784; 3.8077 402.7170; 3.8957 437.5221; 4.0314 506.9907];
m30=[3.6335 0; 3.6373 49.8884; 3.6488 99.5405; 3.6685148.5936; 3.7363 243.2204; 3.7876 287.7398; 3.8537 329.6097; 3.8935 349.9452; 3.9384 368.9776; 3.9892 387.2576; 4.0410 404.5759; 4.1350 439.5416; 4.2153 474.2420; 4.2813 509.3309];
yy = [0:50:500];
xx20 = interp1(m20(:,2),m20(:,1),yy);
xx30 = interp1(m30(:,2),m30(:,1),yy);
for m = 1:9
mm(:,m) = xx20 + (xx30-xx20)*(m/(30-20));
end
plot(m20(:,1),m20(:,2),xx20,yy,xx30,yy,m30(:,1),m30(:,2),mm,yy)
You interpolate the given M vectors to find the x coordinate of a set of y values - these are the interp1 lines. Then, you linearly interpolate as a function of m between the interpolated x-coordinates.
I have the following results :
v =
7.8053 959.5985
6.1820 481.3263
4.9794 242.2347
4.0829 122.7578
3.4079 63.1224
2.8962 33.4578
2.5118 18.8560
2.2380 11.9084
2.0725 8.9597
2.0086 8.0952
2.0001 8.0012
2.0000 8.0000
2.0000 8.0000
Which I got them after I ran a function called newton_system for calculating , and I ran the input vector [10,3] on the follwoing f(x,y) :
y(1) = x(1)^3 - 5*x(1)^4 + x(2)^2 + 8;
y(2) = 2*x(1)^3 - x(2)^2 + 5*x(1)^2 + 5*x(2) - 12;
I ran 13 iterations , and now I want to plot a graph of the approximations as a function of the iterations (from 1 to 13) , can someone please explain how to do that ?
FYI please notice that as I progress with the iterations , we converge to 2.0000 8.0000
Here's a solution using the plot command:
plot(v(:,1),v(:,2),'-o') %# plot the line with circles for the x,y values
hold on,plot(2,8,'+r') %# add a red cross for the solution
xlim([0 8]) %# modify x-axes limits so that the plot looks a bit better
Use the zoom button on the figure menu to zoom into the area around the solution.