Using linsapce between two points in plot to create a non linear line.

8 views (last 30 days)
I have a problem with linspace i want visulize tempeture decline between two points thats not linear between T2 and T3
rluft = r3 + 0.2;
x = [r1, r2, r3, rluft];
y = [T1, T2, T3, Tluft];
plot(app.UIAxes, x, y);
hold(app.UIAxes,"on")
xline(app.UIAxes, r1,'--')
xline(app.UIAxes, r2,'--')
xline(app.UIAxes, r3,'--')
xticks(app.UIAxes, [r1, r2, r3])
ylim(app.UIAxes, [0,100]);
yticks(app.UIAxes, 0:10:100);
hold(app.UIAxes,"off")
  1 Comment
Dyuman Joshi
Dyuman Joshi on 20 Dec 2023
The lin in linspace means linear, so you can't get non-linear points with it.
You will have to use different functions/functionalities according to the non-linearity you want to introduce.

Sign in to comment.

Answers (1)

Hassaan
Hassaan on 21 Dec 2023
To plot a non-linear line between two points, you can create a set of points that follow the desired non-linear relationship and then plot those points. If you wants a specific type of non-linearity, like a quadratic or exponential decline, they can generate the points accordingly.
Here's a basic example of how to create and plot a non-linear line between two points in MATLAB
% Define the range and temperatures
r1 = 0; % Example start range value
r2 = 5; % Example midpoint range value
r3 = 10; % Example end range value
T1 = 100; % Example start temperature value
T2 = 80; % Example midpoint temperature value
T3 = 50; % Example end temperature value
rluft = r3 + 0.2; % Example extended range value
Tluft = 45; % Example extended temperature value
% Define the x and y vectors
x = [r1, r2, r3, rluft];
y = [T1, T2, T3, Tluft];
% Create a finer set of x values between r2 and r3 for the non-linear part
x_fine = linspace(r2, r3, 100);
% Generate a non-linear decline (exponential decay here as an example)
decay_rate = log(T3/T2) / (r3 - r2);
y_fine = T2 * exp(decay_rate * (x_fine - r2));
% Plot the points
plot(x, y, 'o'); % Plot the original points as circles
hold on; % Keep the plot active to overlay the non-linear line
% Plot the non-linear decline
plot(x_fine, y_fine, 'r'); % Plot the non-linear line in red
% Add vertical lines for reference
xline(r1, '--');
xline(r2, '--');
xline(r3, '--');
% Customize the ticks and limits
xticks([r1, r2, r3])
ylim([0, 100]);
yticks(0:10:100);
% Release the hold on the plot
hold off;
In this code, x_fine is a set of x values between r2 and r3 generated with linspace, which are then used to calculate the corresponding y values using an exponential decay formula to simulate a temperature decline. The rate of decay is calculated based on the decline from T2 to T3 over the range from r2 to r3.
This is just an example, and the actual non-linear function used should be chosen based on the specific behavior of temperature decline that the user wants to model. If a different non-linear behavior is desired, the formula for y_fine would need to be adjusted accordingly.
If you liked the anwer and it solved your problem. Please do leave a upvote and a comment means a lot. Thank you.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!