Generate long piecewise function

6 次查看(过去 30 天)
Hello,
I'm trying to implement a piecewise function in a for loop.
Basically I have a linear slope defined by two heights at the ends, and I want to discretize that slope into a piecewise function with an high number of member (10, 30, 100 or input-defined).
The best way I could think of was generating a string through a series of for loop (and a lot of num2str like functions) and copy-pasting it in the argument of a piecewise function, eg:
piecewise(0<=x<=0.16933,0.54111,0.16933<=x<=0.33867,......)
I'm sure there is a better way or a built-in fuction but cannot find anything

回答(2 个)

Guru Mohanty
Guru Mohanty 2020-5-4
Hi, you want to get piecewise value of the slope between two heights. You can use interp1and gradientfunction for this. Here is a sample code for it.
clc;clear all;
t1=0;t2=10; % Define position of two heights
height1=3; % Define Values of Two Heights
height2=5;
n=500; % Set Number of Members
tnew=linspace(t1,t2,n);
y1=interp1([t1 t2],[height1 height2],tnew);
slope=gradient(y1,tnew);

Image Analyst
Image Analyst 2020-5-4
See my piecewise linear fit demo.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by