フィルターのクリア

Create a triangular function

10 ビュー (過去 30 日間)
Mikkel Christensen
Mikkel Christensen 2020 年 4 月 22 日
コメント済み: Mikkel Christensen 2020 年 4 月 22 日
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.

採用された回答

Johannes Hougaard
Johannes Hougaard 2020 年 4 月 22 日
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 件のコメント
Johannes Hougaard
Johannes Hougaard 2020 年 4 月 22 日
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.
Mikkel Christensen
Mikkel Christensen 2020 年 4 月 22 日
Ah, I was entering input in the wrong function and got something quite different ... : |
Well, that works then. Amazing!
Thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Estimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by