Matlab (How to fit multiple data sets ) [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Suppose I have for vectors x1, y1, x2, y2, and I would like to plot this data (x1,y1) and (x2,y2) with different colors. The dimesions of vector x1,y1 are not the same with x2,y2.
Than I would like also to fit all this data together, with the same polynomial fit, degree 1.
Can someone help me to do it?

You can plot the vectors simply using plot:
plot(x1, y1, 'r.', x2, y2, 'b.')
where the 'r.' specifies that this first pair should be plotted in red dots, and the 'b.' specifies that the second pair should be plotted in blue dots. You can find a more complete list of color/marker options in the help documentation for plot.
To fit a polynomial to (x,y) data, you can use polyfit:
poly_coeffs = polyfit( x, y, poly_degree )
If you want to fit the same polynomial to both sets of data, you should concatenate your vectors into a single vector, e.g. (in the case of row vectors):
x = [x1, x2]
y = [y1, y2]
poly_coeffs = polyfit( x, y, poly_degree )
If you have column vectors, you would use x = [x1; x2] (note the semicolon instead of the comma) to concatenate them vertically.
And now if you wanted to plot the polynomial fit on top of the original data, you can add it on to the list of arguments to plot:
curve_x = linspace( min(x), max(x), 100 );
curve_y = polyval( poly_coeffs, curve_x );
plot(x1,y1,'r.', x2,y2,'b.', curve_x,curve_y,'k-');

Related

I need help to solve my matlab problem in plot surface [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Please can anyone help me to solve my Matlab problem. I will share the question and it need to be solved in Matlab. Thanks in advance for your help.
Using meshgrid() to Plot Surface
This method uses two vectors to set the axes. After creating the axes the grid over coordinates to plot over (domain) is created using the meshgrid(). The meshgrid() function will take two vectors that describe the axes. After creating this grid of coordinates the function k can be evaluated for all the points on the grid-pair.
For 6 ≤ n ≤ 12 (ten values) and 0.001 ≤ p ≤ 0.009 (five values):
Here you can see the top row of images showing the vectors used to create the axes. The second row of images shows the grid formation created using meshgrid(). You can see by taking one cell/value from n and one cell value from p a coordinate pair is formed → (n,p). For example, one coordinate point can be (n,p) → (6,0.0030).
For 6 ≤ n ≤ 12 (ten values) and 0.01 ≤ p ≤ 0.1 (ten values):
Number_Of_P_Ticks = 5;
P_Axis = linspace(0.001,0.009,Number_Of_P_Ticks);
Number_Of_N_Ticks = 10;
N_Axis = linspace(6,12,Number_Of_N_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,1); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 5 Values of n");
xlabel("p"); ylabel("n");
Number_Of_P_Ticks = 10;
P_Axis = linspace(0.01,0.1,Number_Of_P_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,2); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 10 Values of n");
xlabel("p"); ylabel("n");
Ran using MATLAB R2019b

Linear interpolation inside a 3D triangle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have data at the nodes of a 3D triangle, and I need to interpolate to get data inside the triangle.
here is what i tried to do:
x=[0,1,0];
y=[1,0,1];
z=[0,2,-1];
[X,Y,Z]=meshgrid(x,y,z);
v=[2,5,-1];
xs=linspace(0,1,.1);
ys=linspace(0,1,.1);
zs=linspace(-1,2,.1);
Vs = interp3(X,Y,Z,v,xs,ys,zs,'linear');
i get an error: The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays.
what is wrong?
Let Xcontain the x-coordinates of your nodes, Y the y-coordinates, Z the z-coordinates of your nodes. Store the value/data at your nodes in V. Now you can specify where you want to interpolate the data by saving the x,y and z-coordinates of these points in Xs,Ys and Zs. The value of your data at these points is:
Vs = interp3(X,Y,Z,V,Xs,Ys,Zs,'linear');
You can take a look at the Matlab documentation.
Edit: As you added your code: The error seems to be that the dimension of your V is wrong. If you take a look at the example Matlab Docu -> interp3 -> Evaluate Outside the Domain of X, Y, and Z you will see, that Vhas to have the dimension as X, Yand Zcombined. From the documentation: size(V) = [length(Y) length(X) length(Z)] for vectors X ,Yand Z.
Here is an example:
X = linspace(-1,2,5);
Y = linspace(-1,7,23);
Z = linspace(3,9,23);
V = rand(23,5,23);
xq = linspace(0,1,34);
yq = linspace(0,2,34);
zq = linspace(4,5,34);
vq = interp3(X,Y,Z,V,xq,yq,zq,'linear',-1);

Plotting the relationship between four variables in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am struggling with something that should not be too hard; I have three variables (x,y,z) and an outcome (e), and I know the relationship between them. Let's for the sake of it assume that the relationship is as follows:
e = x + y.^2 + (4/z)
Now, what I want to do is create a plot in MATLAB that shows me this function, with the three variables on the respective axes, and the color reflecting the outcome (e). I know it probably has something to do with meshgrid and surf, but from the way I see it, those can only plot three variables total, and not four, as in my case.
Thanks in advance for your help,
I tried out using isonormals() and it gave a useful plot:
The red surface corresponds to e = 0, whereas blue and green surfaces show e = 5 and e = -5 respectively.
Here is the code:
clear;
[x, y, z] = meshgrid(-30:0.5:30, -10:0.5:10, -1:0.01:1);
e = x + y.^2 + 4./z;
e1 = 0; e2 = 5; e3 = -5;
e_val = [0, 5, -5];
c_val = ['r', 'b', 'g'];
for i=1:length(e_val)
p = patch(isosurface(x,y,z,e,e_val(i)),'FaceColor',c_val(i),'EdgeColor','none');
isonormals(x,y,z,e,p);
hold on;
end
hold off;
xlabel('X');ylabel('Y');zlabel('Z');
view(3); axis tight;
camlight;
lighting gouraud;

How to plot probability density function? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
i want to plot probability density function for about 7500 data that shows peak ground acceleretion(PGA).what is the MATLAB code for this?thanks.
I think you want to estimate the probability densiry function from your data. That's a histogram, and for that you use the hist function:
data = randn(1,7500); %// example data
n = 21; %// number of bins
[y, x] = hist(data, n);
y = y/(x(2)-x(1))/numel(data); %// normalize so that total area is 1
plot(x, y)
%// To check that area is approximately 1, compute the integral: trapz(x, y)
If you happen to know your distribution (for example Gaussian) you can do this:
data = randn(1,7500);
[mu sigma] = normfit(data);
X = mu - 10 : .1 : mu + 10;
Y = normpdf(X,mu,sigma);
plot(X,Y);
And you could get this,
However if your data isn't Gaussian, you can use other fitting functions.

Smoothing an airfoil data [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
These are some data points for an airfoil shape:
x=[1 0.8518 0.7040 0.5536 0.3988 0.2454 0.0937 0.0199 0.0015 0 0.0169 0.0812 0.2054 0.3525 0.4979 0.6457 0.7974 0.9497];
y=[0 0.0355 0.0819 0.1206 0.1347 0.1200 0.0777 0.0363 0.0162 0 -0.0197 -0.0428 -0.0645 -0.0749 -0.0701 -0.0506 -0.0249 -0.0026];
I'm not allowed to use any curve fitting toolbox. What method do I use to plot a smoother looking airfoil shape. Should I use polyfit or interp1?
Yes, interp1 should do the job, but you need to spilt your data in two halves, the positive y and the negative y, with their corresponding x values.
Here's an example using cubic interpolation. Check the doc for interp1 for more details:
ypos = y(y>=0); % y only when positive
xpos = x(y>=0); % corresponding values of x
yneg = y(y<0); % y only when strictly negative
xneg = x(y<0); % corresponding values of x
xi=linspace(0,max(x),100); % values of x for interpolation (100 values linearly spaced between 0 and the max of x
yposi = interp1(xpos,ypos,xi,'cubic','extrap'); % interpolated values of y (when positive) using cubic interpolation and extrapolation
ynegi = interp1(xneg,yneg,xi,'cubic','extrap'); % interpolated values of y (when strictly negative) using cubic interpolation and extrapolation
plot(x,y,'ro',xi,yposi,'b-',xi,ynegi,'b-') % plot interpolated data on top of original data