Need a tangent line in my plot like the example provided but I cannot seem to do it

10 views (last 30 days)
I am trying to make a tangent or line of best fit t this data but only from the second half of the plot on should be focused on. Below is an example from the professor
Need smething like this...
which translates to something like this...
But for some reason I keep getting this...
By using this code...
%% Calculation Problem 2
%Given Data Problems 1-4
dd = load('NEWPROB6.txt') %Data loading for airfoil
Error using load
Unable to find file or directory 'NEWPROB6.txt'.
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
Does anyone know how I can fix this?
  3 Comments
ryan
ryan on 31 May 2025
Edited: Walter Roberson on 1 Jun 2025
The txt file NewProb6
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

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 31 May 2025
Calculate the numerical derivative, find its value at the desired point on the curve, and calculate the tangent line parameters.
Try this --
x = linspace(0, 15);
f = @(x) 10./(x+0.1);
y = f(x);
dfdx = gradient(y,x);
figure
plot(x, y, DisplayName='f(x)')
hold on
plot(x, dfdx, DisplayName = 'df(x)/dx')
hold off
grid
legend(Location='best')
ylim([-4 4])
title('Data & Derivative')
xpt = 7.5;
ypt = f(xpt);
slope = interp1(x, dfdx, xpt)
slope = -0.1733
intercept = ypt - slope*xpt
intercept = 2.6152
figure
plot(x, y, DisplayName='f(x)')
hold on
plot(xpt, ypt,'sr', MarkerFaceColor='r', DisplayName='Tangent Point')
plot(x, slope*x+intercept, '--r', DisplayName='Tangent Line')
hold off
grid
legend(Location='best')
ylim([0 4])
title('Data & Tangent Line')
.
  2 Comments
Image Analyst
Image Analyst on 31 May 2025
@ryan, tangent line is the slope at a particular point, while a fit is a line through a range of points. Make it clear what you want: the tangent or the fitted line.
Star Strider
Star Strider on 1 Jun 2025
Edited: Star Strider on 1 Jun 2025
Using the provided data , try this --
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
TCL = 1.0280
Talpha = 6 %Target angle of attack (degrees)
Talpha = 6
%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>
dydx = gradient(CrR, Rht);
xpt = Rht(round(numel(CrR)/2));
ypt = interp1(Rht, CrR, xpt);
slope = interp1(Rht, dydx, xpt)
slope = -0.1269
intercept = ypt - slope*xpt
intercept = 1.7837
figure
subplot(2,1,1)
plot(Rht,CrR, 'b.-', 'LineWidth',2, 'MarkerSize', 20)
halfpoints = round(numel(CrR)/2)
halfpoints = 5
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)
RhtHalf = 1×6
7.0833 8.6667 10.2500 11.8333 13.4167 15.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
CrRHalf = CrR(halfpoints : end)
CrRHalf = 1×6
0.8852 0.7271 0.6166 0.5351 0.4725 0.4230
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
coefficients = polyfit(RhtHalf, CrRHalf, 1)
coefficients = 1×2
-0.0570 1.2388
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
RhtFitted = linspace(5, 10, 1)
RhtFitted = 10
CrRFitted = polyval(coefficients, RhtFitted);
plot(Rht, slope*Rht+intercept, '--m', DisplayName='Tangent Line', LineWidth=1.5)
plot(RhtFitted, CrRFitted, 'c--', 'LineWidth', 2)
grid on
legend('Original Data', 'Linear Fit (2nd Half)','Tangent Line');
hold off
.
EDIT -- Minor tweak.
.

Sign in to comment.


Image Analyst
Image Analyst on 1 Jun 2025
RhtFitted = linspace(5, 10, 1)
RhtFitted = 10
gives only a single value, so of course no line will be plotted. You need at least two points to plot a line.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!