How can I place a linear regression line (line of best fit) through both plots in this code, but only from the halfway point to the end?

12 views (last 30 days)
I am trying to fit a linear regression line focusoed on only the second half of both plots in this provided code how can I do so? (for wind turbines, most power made on end of the blade so just the second half of the plot is needed to be focused on), but I would need a linear regression line or in other words a line of best fit similar to this example picture below
For example the solid line is what I already have (for both plots, but for example chord distribution is the first) within the plot and the dashed line is the linear fit line which represents the curve but only focused on the second half. I should've inlcuded this first of all hopefully it clears things up
txt data provided in comments
problem statemnt for example
Code......
% Given Data Problems 1-4
dd = [0.0 0.3983 0.0066 0.0017 -0.0927 0.6330 0.6222
0.5 0.4536 0.0066 0.0018 -0.0926 0.6172 0.6320
1.0 0.5091 0.0068 0.0018 -0.0925 0.6013 0.6389
1.5 0.5641 0.0069 0.0019 -0.0924 0.5857 0.6453
2.0 0.6189 0.0070 0.0020 -0.0923 0.5699 0.6517
2.5 0.6733 0.0072 0.0021 -0.0921 0.5540 0.6577
3.0 0.7270 0.0073 0.0023 -0.0917 0.5358 0.6644
3.5 0.7792 0.0075 0.0024 -0.0911 0.5135 0.6714
4.0 0.8310 0.0078 0.0026 -0.0905 0.4921 0.6782
4.5 0.8822 0.0080 0.0028 -0.0897 0.4682 0.6864
5.0 0.9328 0.0083 0.0031 -0.0889 0.4464 0.6948
5.5 0.9812 0.0086 0.0034 -0.0877 0.4199 0.7041
6.0 1.0280 0.0090 0.0037 -0.0861 0.3896 0.7145
6.5 1.0728 0.0094 0.0041 -0.0843 0.3581 0.7262
7.0 1.1138 0.0100 0.0045 -0.0817 0.3214 0.7394
7.5 1.1459 0.0107 0.0051 -0.0776 0.2766 0.7556
8.0 1.1724 0.0117 0.0059 -0.0726 0.2228 0.7750
8.5 1.1957 0.0128 0.0068 -0.0673 0.1745 0.7994
9.0 1.2193 0.0139 0.0079 -0.0622 0.1382 0.8326
9.5 1.2403 0.0149 0.0089 -0.0568 0.1102 0.9219
10.0 1.2659 0.0161 0.0102 -0.0528 0.0907 0.9825
10.5 1.2906 0.0175 0.0114 -0.0489 0.0728 1.0000
11.0 1.3137 0.0191 0.0130 -0.0452 0.0602 1.0000
11.5 1.3367 0.0208 0.0147 -0.0419 0.0518 1.0000
12.0 1.3589 0.0226 0.0166 -0.0387 0.0458 1.0000
12.5 1.3796 0.0247 0.0187 -0.0358 0.0412 1.0000
13.0 1.3951 0.0273 0.0213 -0.0328 0.0368 1.0000
13.5 1.4159 0.0297 0.0238 -0.0307 0.0345 1.0000
14.0 1.4268 0.0330 0.0271 -0.0282 0.0316 1.0000
14.5 1.4433 0.0360 0.0303 -0.0266 0.0300 1.0000
15.0 1.4559 0.0396 0.0340 -0.0251 0.0282 1.0000
15.5 1.4590 0.0443 0.0388 -0.0239 0.0264 1.0000
16.0 1.4703 0.0485 0.0431 -0.0233 0.0253 1.0000
16.5 1.4782 0.0532 0.0480 -0.0231 0.0241 1.0000
17.0 1.4794 0.0590 0.0539 -0.0233 0.0230 1.0000
17.5 1.4707 0.0663 0.0614 -0.0242 0.0220 1.0000
18.0 1.4743 0.0723 0.0676 -0.0254 0.0214 1.0000
18.5 1.4732 0.0792 0.0746 -0.0272 0.0208 1.0000
19.0 1.4701 0.0867 0.0822 -0.0294 0.0201 1.0000
19.5 1.4626 0.0950 0.0907 -0.0324 0.0196 1.0000
20.0 1.4501 0.1043 0.1002 -0.0360 0.0190 1.0000];
alpha = dd(:,1); % Angle of attacks (Degreed)
CL = dd(:,2); % Lift coefficient
CD = dd(:,3); % Drag coefficient
Rh = 0.75; % Turbine hub radius (m)
Rt = 15; % Turbine blade tip radius (m)
Rht = linspace(Rh,Rt,10); % r/R radius hub to tip (m)
Rr = Rht/Rt;
B = 3; % Number of blades on the turbine
TSR = 8; % Tips-speed ratio
% Calculation Problem 1
CLD = CL./CD % Lift-to-Drag Ratio
CLD = 41×1
60.3485 68.7273 74.8676 81.7536 88.4143 93.5139 99.5890 103.8933 106.5385 110.2750 112.3855 114.0930 114.2222 114.1277 111.3800
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
TCL = 1.0280; % Lift coefficient associated with maximum lift-to-drag ratio
Talpha = 6; % Target angle of attack (degrees)
%Calculation Problem 2
Phi = atand(2./(3*TSR*(Rr))) % Local inflow angle (degrees)
Phi = 1×10
59.0362 28.1786 17.7004 12.8043 10.0080 8.2072 6.9530 6.0300 5.3228 4.7636
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
CrR = Rt*(8*pi*sind(Phi))./(3*B*TCL*TSR); % chord distribution
Beta = Phi-Talpha
Beta = 1×10
53.0362 22.1786 11.7004 6.8043 4.0080 2.2072 0.9530 0.0300 -0.6772 -1.2364
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
subplot(2,1,1)
plot(Rht, CrR, 'LineWidth', 2)
xlabel('r/R')
ylabel('Chord Distribution (m)')
grid on
subplot(2,1,2)
plot(Rht, Beta, 'LineWidth', 2)
xlabel('r/R')
ylabel('Beta (degrees)')
grid on
% Problem 3 Linear Fit Line for Rht/CrR & Rht/Beta?
  6 Comments
ryan
ryan on 30 May 2025
Edited: ryan on 30 May 2025
I am trying to fit a linear regression line focusoed on only the second half of a plot (for wind turbines, most power made on end of the blade so just the second half of the plot is needed to be focused on considering the x axis is the lenght of the wind turbine blade for both plots for example), but I would need a linear regression line or in other words a line of best fit similar to this example
For example the solid line is what I already have (for both plots, but for example chord distribution is the first) within the plot and the dashed line is the linear fit line which represents the curve but only focused on the second half. I need helpo figuring out how to create that dashed line (line of best fit) but to the conditions that it;'s at withi the picture (focused only on second half) should've inlcuded this first of all hopefully it clears things up

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 29 May 2025
Do you already have the "linear distribution" and "chord and twist distribution"? Is that what columns 2 and 3 are?
You are right that the tool to plot those distributions is plot(), or at least that's one way - you could also use bar(). If you don't already have those two distributions in variables, then I have no idea how to "determine suitable" vectors for them from your Problem 2 (which was not supplied to us here), and you should ask your instructor for help with that.
To plot only half of a vector you can do this
halfPoints = round(numel(y) / 2)
plot(x(1 : halfPoints), y(1:halfPoints), 'b-', 'LineWidth', 2);
grid on;
  1 Comment
ryan
ryan on 30 May 2025
I believe I provided some more information ina comment above me that may be helpful if not elt me now thank you so much for helping me

Sign in to comment.


Sam Chak
Sam Chak on 30 May 2025
In purely math terms, do you mean to find the tangent line at the midpoint of the decaying beta curve? If so, I used math techniques to calculate the midpoint and draw the straight line. Of course, you can also use some built-in curve-fitting tools.
% Given Data Problems 1-4
dd = [0.0 0.3983 0.0066 0.0017 -0.0927 0.6330 0.6222
0.5 0.4536 0.0066 0.0018 -0.0926 0.6172 0.6320
1.0 0.5091 0.0068 0.0018 -0.0925 0.6013 0.6389
1.5 0.5641 0.0069 0.0019 -0.0924 0.5857 0.6453
2.0 0.6189 0.0070 0.0020 -0.0923 0.5699 0.6517
2.5 0.6733 0.0072 0.0021 -0.0921 0.5540 0.6577
3.0 0.7270 0.0073 0.0023 -0.0917 0.5358 0.6644
3.5 0.7792 0.0075 0.0024 -0.0911 0.5135 0.6714
4.0 0.8310 0.0078 0.0026 -0.0905 0.4921 0.6782
4.5 0.8822 0.0080 0.0028 -0.0897 0.4682 0.6864
5.0 0.9328 0.0083 0.0031 -0.0889 0.4464 0.6948
5.5 0.9812 0.0086 0.0034 -0.0877 0.4199 0.7041
6.0 1.0280 0.0090 0.0037 -0.0861 0.3896 0.7145
6.5 1.0728 0.0094 0.0041 -0.0843 0.3581 0.7262
7.0 1.1138 0.0100 0.0045 -0.0817 0.3214 0.7394
7.5 1.1459 0.0107 0.0051 -0.0776 0.2766 0.7556
8.0 1.1724 0.0117 0.0059 -0.0726 0.2228 0.7750
8.5 1.1957 0.0128 0.0068 -0.0673 0.1745 0.7994
9.0 1.2193 0.0139 0.0079 -0.0622 0.1382 0.8326
9.5 1.2403 0.0149 0.0089 -0.0568 0.1102 0.9219
10.0 1.2659 0.0161 0.0102 -0.0528 0.0907 0.9825
10.5 1.2906 0.0175 0.0114 -0.0489 0.0728 1.0000
11.0 1.3137 0.0191 0.0130 -0.0452 0.0602 1.0000
11.5 1.3367 0.0208 0.0147 -0.0419 0.0518 1.0000
12.0 1.3589 0.0226 0.0166 -0.0387 0.0458 1.0000
12.5 1.3796 0.0247 0.0187 -0.0358 0.0412 1.0000
13.0 1.3951 0.0273 0.0213 -0.0328 0.0368 1.0000
13.5 1.4159 0.0297 0.0238 -0.0307 0.0345 1.0000
14.0 1.4268 0.0330 0.0271 -0.0282 0.0316 1.0000
14.5 1.4433 0.0360 0.0303 -0.0266 0.0300 1.0000
15.0 1.4559 0.0396 0.0340 -0.0251 0.0282 1.0000
15.5 1.4590 0.0443 0.0388 -0.0239 0.0264 1.0000
16.0 1.4703 0.0485 0.0431 -0.0233 0.0253 1.0000
16.5 1.4782 0.0532 0.0480 -0.0231 0.0241 1.0000
17.0 1.4794 0.0590 0.0539 -0.0233 0.0230 1.0000
17.5 1.4707 0.0663 0.0614 -0.0242 0.0220 1.0000
18.0 1.4743 0.0723 0.0676 -0.0254 0.0214 1.0000
18.5 1.4732 0.0792 0.0746 -0.0272 0.0208 1.0000
19.0 1.4701 0.0867 0.0822 -0.0294 0.0201 1.0000
19.5 1.4626 0.0950 0.0907 -0.0324 0.0196 1.0000
20.0 1.4501 0.1043 0.1002 -0.0360 0.0190 1.0000];
alpha = dd(:,1); % Angle of attacks (Degreed)
CL = dd(:,2); % Lift coefficient
CD = dd(:,3); % Drag coefficient
Rh = 0.75; % Turbine hub radius (m)
Rt = 15; % Turbine blade tip radius (m)
Rht = linspace(Rh, Rt, 10001); % r/R radius hub to tip (m)
Rr = Rht/Rt;
B = 3; % Number of blades on the turbine
TSR = 8; % Tips-speed ratio
% Calculation Problem 1
CLD = CL./CD; % Lift-to-Drag Ratio
TCL = 1.0280; % Lift coefficient associated with maximum lift-to-drag ratio
Talpha = 6; % Target angle of attack (degrees)
%Calculation Problem 2
Phi = atand(2./(3*TSR*(Rr))); % Local inflow angle (degrees)
CrR = Rt*(8*pi*sind(Phi))./(3*B*TCL*TSR); % chord distribution
Beta = Phi-Talpha;
midpoint= mean([Rh, Rt])
midpoint = 7.8750
idx = find(abs(Rht - midpoint) < 1e-3)
idx = 5001
dBeta = gradient(Beta)./gradient(Rht);
m = dBeta(idx) % slope
m = -1.1265
c = Beta(idx) - m*Rht(idx) % y-intercept
c = 11.8904
point1 = m*Rht(1) + c;
point2 = m*Rht(end) + c;
subplot(2,1,1)
plot(Rht, CrR, 'LineWidth', 2)
xlabel('r/R')
ylabel('Chord Distribution (m)')
grid on
subplot(2,1,2)
hold on
plot(Rht, Beta, 'LineWidth', 2)
plot([Rht(1) Rht(end)], [point1 point2], '--') % tangent line
plot(Rht(idx), Beta(idx), 'o', 'LineWidth', 2) % midpoint
hold off
xlabel('r/R')
ylabel('Beta (degrees)')
grid on
  1 Comment
ryan
ryan on 30 May 2025
Edited: Image Analyst on 30 May 2025
Hello @Sam Chak I tried your answer with this code right here...
%Given Data Problems 1-4
dd = load('NEWPROB6.txt') %Data loading for airfoil
alpha = dd(:,1); %Angle of attacks (Degreed)
CL = dd(:,2); %Lift coefficient
CD = dd(:,3); %Drag coefficient
Rh = 0.75; %Turbine hub radius (m)
Rt = 15; %Turbine blade tip radius (m)
Rht = linspace(Rh,Rt,10); %r/R radius hub to tip (m)
Rr = Rht/Rt;
B = 3; %Number of blades on the turbine
TSR = 8; %Tips-speed ratio
%Calculation Problem 1
CLD = CL./CD %Lift-to-Drag Ratio
TCL = 1.0280 %Lift coefficient associated with maximum lift-to-drag ratio
Talpha = 6 %Target angle of attack (degrees)
%Calculation Problem 2
Phi = atand(2./(3*TSR*(Rr))) %Local inflow angle (degrees)
CrR = Rt*(8*pi*sind(Phi))./(3*B*TCL*TSR); %chord distribution
Beta = Phi-Talpha
midpoint = mean([Rh, Rt])
idx = find(abs(Rht - midpoint) < 1e-3)
dBeta = gradient(Beta)./gradient(Rht);
m = dBeta(idx)
c = Beta(idx) - m.*Rht(idx)
point1 = m*Rht(1) + c;
point2 = m*Rht(end) + c;
subplot(2,1,1)
plot(Rht,CrR, 'LineWidth',2)
xlabel('r/R')
ylabel('Chord Distribution (m)')
grid on
hold on
subplot(2,1,2)
hold on
plot(Rht, Beta, 'LineWidth', 2)
plot([Rht(1) Rht(end)], [point1 point2], '--') % tangent line
plot(Rht(idx), Beta(idx), 'o', 'LineWidth', 2) % midpoint
xlabel('r/R')
ylabel('Beta (degrees)')
grid on
The plot I'm receiving
Error I'm receiving..

Sign in to comment.


Image Analyst
Image Analyst on 30 May 2025
Moved: Image Analyst on 30 May 2025
Maybe try this (untested):
% x and y are your variables. Replace them with whatever variable names you actually have.
plot(x, y, 'b-', 'LineWidth', 2); % Plot full curve
halfPoints = round(numel(y) / 2)
% Let's put a red vertical line at the half way point.
hold on;
xline(x(halfPoints), 'Color', 'r')
% Get the right half of the data.
xHalf = x(halfPoints : end);
yHalf = y(halfPoints : end);
% Fit the right half of the data to a line with polyfit
coefficients = polyfit(xHalf, yHalf, 1);
% Extend that line and get a vector covering
% the whole original range of x.
xFitted = linspace(min(x), max(x), numel(x)); % Linearly spaced x samples.
%xFitted = x; % Or, you can use the same x as the original if you want.
% Get fitted y values by calling polyfit.
yFitted = polyval(coefficients, xFitted);
% Plot the fitted line as a cyan dashed line.
plot(xFitted, yFitted, 'c--', 'LineWidth', 2);
grid on;
legend('Original', 'Halfway line', 'Fitted', 'Location', 'north');
hold off;
  5 Comments
ryan
ryan on 30 May 2025
Hello @Image Analyst I changed the word coefficients, the linspace is a range of values starting with the smallest radius to the largest radius so that why its set from Rht to 1, but for some reason still getting this vertical line below the picture is my new code. I am not clearly surfe what to do from your guidance but am grateful for what youve given me thank you
%% Calculation Problem 2
%Given Data Problems 1-4
dd = load('NEWPROB6.txt') %Data loading for airfoil
alpha = dd(:,1); %Angle of attacks (Degreed)
CL = dd(:,2); %Lift coefficient
CD = dd(:,3); %Drag coefficient
Rh = 0.75; %Turbine hub radius (m)
Rt = 15; %Turbine blade tip radius (m)
Rht = linspace(Rh,Rt,10); %r/R radius hub to tip (m)
Rr = Rht/Rt;
B = 3; %Number of blades on the turbine
TSR = 8; %Tips-speed ratio
%Calculation Problem 1
CLD = CL./CD %Lift-to-Drag Ratio
TCL = 1.0280 %Lift coefficient associated with maximum lift-to-drag ratio
Talpha = 6 %Target angle of attack (degrees)
%Calculation Problem 2
Phi = atand(2./(3*TSR*(Rr))) %Local inflow angle (degrees)
CrR = Rt*(8*pi*sind(Phi))./(3*B*TCL*TSR); %chord distribution
Beta = Phi-Talpha
subplot(2,1,1)
plot(Rht,CrR, 'b.-', 'LineWidth',2, 'MarkerSize', 20)
halfpoints = round(numel(CrR)/2)
xlabel('r/R')
ylabel('Chord Distribution (m)')
grid on
hold on
% Assume x and y are your data vectors
xline(Rht(halfpoints), 'Color', 'r')
% Get halfway index
RhtHalf = Rht(halfpoints : end)
CrRHalf = CrR(halfpoints : end)
coefficients = polyfit(RhtHalf, CrRHalf, 1)
RhtFitted = linspace(5, 10, 1)
CrRFitted = polyval(coefficients, RhtFitted);
plot(RhtFitted, CrRFitted, 'c--', 'LineWidth', 2)
grid on
legend('Original Data', 'Linear Fit (2nd Half)');
hold off
Image Analyst
Image Analyst on 31 May 2025
Why are you doing this
RhtFitted = linspace(5, 10, 1)
instead of the way I told you to do it? that literally gives only one points so there is only one output point to plot. Just watch:
RhtFitted = linspace(5, 10, 1)
RhtFitted = 10
See? Just one point! Why are you only fitting from 5 to 10 instead of the right half of the curve?
I literally ran my code in Answers and it plotted correctly.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!