Smoothing an airfoil data [closed] - matlab

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

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

Assign a value to each element of a matrix inside a given region in Matlab [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 5 years ago.
Improve this question
I have an y*x matrix in matlab, and I want to assign a value to any point of the region of the matrix between 4 points, like A,B,C,D the image of which I know the coordinates, if the point were aligned this wouldn't be a problem, but they aren't.
Is there a function to do this ?
Edit
I have an an input matrix of numbers between 0 and 1.
The size of the matrix is 720*1280
The region is defined like this
x = [3 10 27 20 3];
y = [10 40 31 1 10];
It doesn't matter too much if the pixels are excluded or included along the edge of the rectangle, but is better if they are included.
The output matrix should be equal to the input matrix but with the values of the points inside the region of interest replaced by some other value, for example 2.
The solution I was searching for is very similar to the one that m7913d gave me.
You can use poly2mask to convert the polygon to a mask and then change the desired elements in the matrix using logical indexing as follows:
% Create a sample matrix
matrix = rand(50, 50);
% Define your region as a polygon
x = [3 10 27 20 3];
y = [10 40 31 1 10];
% Convert the polygon to a mask
mask = poly2mask(x,y,size(matrix, 1),size(matrix, 2));
% Change the elements in matrix which are inside the polygon
matrix(mask) = 123;
% Display the result
figure
imshow(matrix, 'InitialMagnification', 'fit')
hold on
plot(x, y, 'r')

Matlab (How to fit multiple data sets ) [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
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-');

How to solve an equation with two variables in MATLAB [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 8 years ago.
Improve this question
For example, I want to solve equation |x|+|y|=1 numerically, I would to get a rectangle. If I plot it out, it should be something like
It's a simple example. What I really want to solve is a equation like sin(x)sin(2y)+sin(y)sin(2x)=0.
It really pays to do a little math before jumping to fancy graphs or numerical solvers.
Suppose you have
sin(x)·sin(2y) + sin(y)·sin(2x) = 0
From any standard list of trig identities you find that this can be re-written as
sin(x)·sin(y) · (cos(x) + cos(y)) = 0
So your equation holds when
sin(x)·sin(y) = 0 or
cos(x) + cos(y) = 0
in other words,
x = 0 ± k·π, or
y = 0 ± k·π, or
x = -y ± 2k·π
with k ∈ ℤ0.
Graphically, this would be a set of vertical lines (x = 0 ± k·π), a set of horizontal lines (y = 0 ± k·π) and a set of lines at ±45° (y = -x ± 2k·π).
Not a line of code needed.
With your example equation, Rody's answer provides an analytic solution.
In general, however, given an equation f(x,y)=0 it may not be possible to find an analytic solution. In that case I suggest:
Define a target x, y area and generate random samples within it.
Compute z = abs(f(x,y)) at the sample points.
Sort the values of z and define the approximate solution set as a given proportion of the lowest values.
f = #(x,y) sin(x).*sin(2*y)+sin(y).*sin(2*x); %// your example function
N = 1e7; %// number of samples
proportion = 1e-2; %// choose to achieve thin lines in solution set
xmin = -10; xmax = 10; %// x limits of target area
ymin = -10; ymax = 10; %// y limits of target area
x = xmin+(xmax-xmin)*rand(1,N);
y = ymin+(ymax-ymin)*rand(1,N);
z = abs(f(x,y));
[zsort isort] = sort(z);
n = round(N*proportion);
plot(x(isort(1:n)),y(isort(1:n)),'b.','markersize',.1)
axis square

coloring specific point according to a given parameter [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 9 years ago.
Improve this question
I am new to matlab coding and I know it's simple that's why I'm trying to plot 2D datas read from a file,but I'm stuck,with my following code I'm reading the x y coordinates and trying to plot specific point with specific indexing according to given criteria ( which is p in my case I won't go too much into details), all I want to know is how can I modify the code so that I can give the correct index to the point that I want to color(i.e when the condition is satisfied plot this specific points with blue or watever),here is my code :
M=load('data1.XYZ');
x=M(:,1); %all x coordinates
y=M(:,2); %all y coordinates
xA=M(1:400,1); % x of particles A
yA=M(1:400,2); % y of particles A
xB=M(401:800,1); % x of particles B
yB=M(401:800,2); % y of particles B
Pos1=[x y]; % read in the x y coordinates
[num1,junk1] = size(Pos1);
PosA=[xA yA]; % read in the x y A coordinates
PosB=[xB yB]; % read in the x y B coordinates
[numA,junkA] = size(PosA);
[numB,junkB] = size(PosB); %no of all B particles
fprintf('Determining Distances between particles...\n');
r = zeros(numA,1);
psil_avg=0.0+0.0i;
psir_avg=0.0+0.0i;
for m=1:numA
for n=1:numA
cnt_l=0;
psi_l=0.0+0.0i;
if(m~=n)
r(m,n)=norm(PosA(m,:)-PosA(n,:));
if(r(m,n)< 1.44)
v1=PosA(m,:)-PosA(n,:);
u=[0 1];
dot=v1(:,1).*u(:,1)+v1(:,2).*u(:,2);
N=norm(v1);
cosinus=dot/N;
theta=acos(cosinus);
cnt_l=cnt_l+1;
psi_l=psi_l+(cos(theta)+6.0i*sin(theta));
psil_avg=psi_l/cnt_l;
for k=1:numA
cnt_r=0;
psi_r=0.0+0.0i;
if(m~k)
r(m,k)=norm(PosA(m,:)-PosA(k,:));
if(r(m,k)< 1.44)
v2=PosA(m,:)-PosA(k,:);
u2=[0 1];
dot2=v2(:,1).*u2(:,1)+v2(:,2).*u2(:,2);
N2=norm(v2);
cosinus2=dot2/N2;
theta2=acos(cosinus);
cnt_r=cnt_r+1;
psi_r=psi_r+(cos(theta2)+6.0i*sin(theta2));
psir_avg=psi_r/cnt_r;
p=sqrt(psi_r*psi_l);
if p > 0.94
% fprintf('bond order parameter is %f\n',p);
plot(xA(n),yA(n),'ro','Markersize',6);
hold on;
else
plot(xA(n),yA(n),'go','Markersize',8);
end
end
end
end
end
end
end
end
if anyone can help I'd be thankful
Use scatter and the following properties:
'MarkerEdgeColor' — Marker edge color
[0 0 1] (blue) (default) | 'auto' | 'none' | three-element RGB vector | string
'MarkerFaceColor' — Marker face color
'none' (default) | 'auto' | three-element RGB vector | string
Your code is a bit hard to read so I will adress this quiestion in general.
If you have groups of coordinates you would like to plot with different colors say X1,Y1 and X2,Y2 you may do the following
figure
plot(X1,Y1,'r*')
hold on
plot(X2,Y2,'b*')
hold off
This will color the first group in red with a dot and the second with blue and a dot.
The hold on, hold off is made to plot more then one plot on a single axis without clearing the previous one.
As suggested in the comments - you should probably try to avoid looping in matlab.
I could not understand your question clearly. Do you mean like this ? For example if I have two plots
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
Do you want different colours say red for 10,blue for 20, green for 30 degrees for both curves ? Is this what you want ? Please specify properly.