Clear Filters
Clear Filters

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

22 views (last 30 days)
Avenger2020
Avenger2020 on 23 Nov 2020
Answered: Deepak on 10 Oct 2024 at 13:56
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

Answers (1)

Deepak
Deepak on 10 Oct 2024 at 13:56
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.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!