Generate long piecewise function

3 visualizaciones (últimos 30 días)
Walter Biribicchi
Walter Biribicchi el 1 de Mayo de 2020
Respondida: Image Analyst el 4 de Mayo de 2020
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

Respuestas (2)

Guru Mohanty
Guru Mohanty el 4 de Mayo de 2020
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 el 4 de Mayo de 2020
See my piecewise linear fit demo.

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by