how to change x and y values to a range of values

6 次查看(过去 30 天)
How can i change the variables X and Y values to a range of values in the code below? Its already in a loop and i used "hold on" to keep the plots previosly so the results are combined in one graph. Thanks in advance. In the code below i used one value for X and one value for Y, but i'd like to use the Y value range that i commented in the code.
clear all
clc
Vinf=100; % Input Vinf
a=deg2rad(15); % Input Alpha in degrees
N=100; % Number of Free Vortexes
c=1; % Length of chord c
X=-2*c;
% Y=linspace(-.7*c,0.1*c,20);
Y=-.7*c;
DeltaT=4e-8;
k=nonzeros(0:c/N:1);
for i=1:length(k)
s(i)=k(i); % si
s1=k(i)-(c/N); % si-1
% s2 is the center of free vortex i
s2=(s(i)+s1)*0.5;
ai=(s2*cos(a));
bi=((-s2)*sin(a));
Thetai=acos(1-2*s(i)); % θi
Thetai_1=acos(1-2*s1); % θi-1
G1(i)=(a*Vinf)*(Thetai-Thetai_1);
G2(i)=(2*a*Vinf)*[(sqrt(s(i)-s(i).^2))-(sqrt(s1-s1.^2))];
% Stength Gi of free vortex "i"
G(i)=G1(i)+G2(i);
u(i)=(G(i)/(2*pi))*((Y-bi)/((X-ai)^2+(Y-bi)^2));
v(i)=((-G(i))/(2*pi))*((X-ai)/((X-ai)^2+(Y-bi)^2));
end
UDT=u*DeltaT;
VDT=v*DeltaT;
DD=X+UDT;
D2=Y+VDT;
plot(DD, D2,'-or')
hold on

回答(1 个)

Deepak
Deepak 2024-10-10
As I understand, you have written a MATLAB script to plot multiple graphs on the same figure using the “hold on” property of MATLAB. Now, you want to change the variable Y to a range of values instead of a constant value.
To accomplish this, we can iterate over the range of values for Y, and for each value of Y, iterate over the values of “k” to plot “X” against different “Y” values.
Additionally, to plot graphs for range of values for both “X” and “Y”, we can use nested loops to iterate over the range of values of “X” and “Y, and for each value, iterate over the “k” values to plot multiple graphs in same figure.
Below is the MATLAB code to accomplish this task:
clear all;
clc
Vinf = 100; % Input Vinf
a = deg2rad(15); % Input Alpha in degrees
N = 100; % Number of Free Vortexes
c = 1; % Length of chord c
X = -2 * c;
Y_values = linspace(-0.7 * c, 0.1 * c, 20); % Range of Y values
DeltaT = 4e-8;
k = nonzeros(0:c/N:1);
% Loop over each Y value
for j = 1:length(Y_values)
Y = Y_values(j);
for i = 1:length(k)
s(i) = k(i); % si
s1 = k(i) - (c/N); % si-1
% s2 is the center of free vortex i
s2 = (s(i) + s1) * 0.5;
ai = (s2 * cos(a));
bi = ((-s2) * sin(a));
Thetai = acos(1 - 2 * s(i)); % θi
Thetai_1 = acos(1 - 2 * s1); % θi-1
G1(i) = (a * Vinf) * (Thetai - Thetai_1);
G2(i) = (2 * a * Vinf) * [(sqrt(s(i) - s(i).^2)) - (sqrt(s1 - s1.^2))];
% Strength Gi of free vortex "i"
G(i) = G1(i) + G2(i);
u(i) = (G(i) / (2 * pi)) * ((Y - bi) / ((X - ai)^2 + (Y - bi)^2));
v(i) = ((-G(i)) / (2 * pi)) * ((X - ai) / ((X - ai)^2 + (Y - bi)^2));
end
UDT = u * DeltaT;
VDT = v * DeltaT;
DD = X + UDT;
D2 = Y + VDT;
plot(DD, D2, '-o')
hold on
end
xlabel('X-axis')
ylabel('Y-axis')
title('Combined Plots for Different Y Values')
grid on
Attaching the documentation of linspace and plot function in MATLAB for reference:
I hope this helps resolving the issue.

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by