2 views (last 30 days)

Hello, I have plotted a few figures as shown below. Now i would like to plot the tangents to each of these curves. How can i do that?

I have searched on the forum but could not get any definitive answer.

The code for this graph is divided in 2 functions as shown below:

function [x,y_val] = plot_trial(Z)

g = 9.8; K = 0.3; % g is the gravitational accleration

y = @(V_d) (1/(2*g*K^2))*(V_d.^2/Z^2);

x = linspace(0,1,100);

y_val = y(x);

end

clc; clear; close all;

% Use the other function 'plot_trial()'

[v_d1, y_val1] = plot_trial(1);

[~,y_val2]= plot_trial(2);

[~,y_val3]= plot_trial(3);

figure

hold on

plot(v_d1, y_val1,v_d1,y_val2,v_d1,y_val3)

legend('Z=1','Z=2','Z=3')

title('Der Tank (Konstante Querschnittsfläche)')

xlabel('dV1/dt')

ylabel('Y')

Star Strider
on 20 May 2019

Edited: Star Strider
on 20 May 2019

At what point do you want to plot the tangent?

The usual approach is to use the gradient function to find the slope, then use that and the value of the function at that point to calculate the intercept. The slope and intercept then define the tangent line at that point.

Example —

x = linspace(0, 5);

y = x.^2;

h = x(2)-x(1);

dydx = gradient(y, h);

xi = 2.1; % Choose An ‘x’ Value

yi = interp1(x, y, xi); % Corresponding ‘y’ Value

dydxi = interp1(x, dydx, xi); % Derivative At ‘x’

intcpt = yi - dydxi*xi; % Calculate Y-Intercept

figure

plot(x, y, '-b')

hold on

plot(x, dydxi*x+intcpt, '-r')

hold off

grid

legend('Data','Tangent', 'Location','NW')

EDIT —

To clarify my approach, I choose to use interp1 and gradient simply because it allows the desired ‘x’ value to be any value between the limits of the x-axis.

KALYAN ACHARJYA
on 20 May 2019

Hello Yash strickly follow the answer of @Star Strider & @Adam

Here I have done for one curve, Here I have used the answer from here, do the same for other two curves. You can use gradient.

% Use the other function 'plot_trial()'

[v_d1, y_val1] = plot_trial(1);

[~,y_val2]= plot_trial(2);

[~,y_val3]= plot_trial(3);

figure

hold on

plot(v_d1, y_val1,v_d1,y_val2,v_d1,y_val3)

%-------------------------

dy=diff(y_val1)./diff(v_d1)

k=60; % Which point you want Change accordingly and check

tang=(v_d1-v_d1(k))*dy(k)+y_val1(k)

hold on

plot(v_d1,tang,'r','linewidth',3)

scatter(v_d1(k),y_val1(k))

legend('Z=1','Z=2','Z=3')

title('Der Tank (Konstante Querschnittsfläche)')

xlabel('dV1/dt')

ylabel('Y')

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.