Create a triangular function

Hello.
I want to create a function in MatLab, which represents the image below.
Now, I can easily create a linear function for one of the lines and set the remaining to some constant, but I fail to do it for the combined two.
I was thinking about making a piecewise function or perhaps an inequality with the following restrictions:
I simply can't get it to work for both. If you have an idea, I'd love to hear it.

 采纳的回答

Hi Mikkel
I'm not quite sure whether I have understood how much of a mathematical function you would prefer for the graph to be.
But if it should just be a practical solution to the problem in a parametrical, piecewise way. I think this function (attached as m-file). Feel free to use it without any credits, copyright or whatever - and change everything you care to change :D
% Example data roughly taken from your question
x = 0:2500;
cutoff = 1250;
maxwidth = 1250;
minwidth = 100;
And here is the function in clear text (same as attached)
function y = triangularplot(x,maxwidth,minwidth,cutoff)
% TRIANGULARPLOT
% Creates a triangular plot in response to MATLAB Central question 519827
%
% I/O
% y = triangularplot(x,maxwidth,minwidth,cutoff);
%
% INPUT
% x = the x-data to use for the calculation
% maxwidth = the width at x = 0
% minwidth = the minimum width of the channel
% cutoff = the x value at which the width reaches the minimum
%
% 2020-04-2020 Johannes Hougaard <xjhh@novonordisk.com>
% Preallocation of y
y = nan(size(x));
% Calculating the slope for the linear section of the graph
slope = (minwidth-maxwidth)/cutoff;
% Calculating the y-values for x <= cutoff
y(x <= cutoff) = maxwidth + x(x <= cutoff).*slope;
% Setting the y-values aboce cutoff as the minimum width
y(x > cutoff) = minwidth;
% Plotting the result
figure;
plot(x,y,'k-');
set(gca,'NextPlot','add');
plot(x,-y,'k-');
set(gca,'NextPlot','replace');
ylabel('Width (km)');

5 个评论

Thank you so much for this - that is quite cool.
I might have formulated my question poorly, but I think it is much simplere.
So, I need a function for the width of a glacier. I would like that function to represent the image above.
Basically a mathematical function
and similary for the other side of the triangle
I need it as one function if that make sense.
Should it be a peicewise function with the intervals ?
Would that work?
Ah - it relates to the value of 'y' not of 'x' - I misread that but I can't see how you would make conditions on the output rather than on your input.
e.g. what is is that or is that ?
Based on the description you give I have a hard time seeing how you would ever reach the 'constant' part of the image shown.
Yea, I see what you mean. It seems like I simple problem, but can't get it right.
For the constant part, I would let it be equal to | 100 m | from (1200:end).
But that is exactly what you get from the above function if you run it as.
y = triangularplot(0:2500,1250,100,1200);
I just still don't get which value you want at e.g. or
if you want the function I sent you to be simpler (just the input a) that would be fairly easy keeping most of the code and just changing the first few lines and hard-coding some stuff - but I'm still not quite sure what the math should be.
function y = triangularplot(x,a)
% TRIANGULARPLOT
% Creates a triangular plot in response to MATLAB Central question 519827
%
% I/O
% y = triangularplot(x,maxwidth,minwidth,cutoff);
%
% INPUT
% x = the x-data to use for the calculation
% a = the constant 'slope' of the initial part
%
% 2020-04-2020 Johannes Hougaard <xjhh@novonordisk.com>
% Preallocation of y
maxwidth = 1250;
minwidth = 100;
y = nan(size(x));
cutoff = (maxwidth - minwidth)/a;
% Calculating the y-values for x <= cutoff
y(x <= cutoff) = maxwidth - a .* x(x <= cutoff);
% Setting the y-values aboce cutoff as the minimum width
y(x > cutoff) = minwidth;
% Plotting the result
figure;
plot(x,y,'k-');
set(gca,'NextPlot','add');
plot(x,-y,'k-');
set(gca,'NextPlot','replace');
ylabel('Width (km)');
could also be that you're simply looking for the built-in function 'sign' which could be multiplied to your a.
Ah, I was entering input in the wrong function and got something quite different ... : |
Well, that works then. Amazing!
Thank you so much

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by