ursmooth
ursmooth

Reputation: 311

Plot a function then plot a constant value of that function

I want to plot a function in matlab so when I reach a certain value of X, I want the function to become constant, taking the value of the function at that value of X. So for this graph for example, I want the it to plot normally all the way to x=60 and from x=60 to x=180 I want it to take the value of f(60), so it becomes like a "L" shaped function. Is this possible?

I have tried to use the unit step function, that didnt work, and also combine 2 vectors, that was messy and didnt work either. enter image description here

clear all
clc



X=0:0.001:180;

S_f=1-4*(sind(X)).^(2);

plot(X,S_f)

Upvotes: 0

Views: 445

Answers (2)

Suever
Suever

Reputation: 65460

You can just set the middle part of the function to the value that you wish. You can use logical indexing to select the middle region of S_f.

S_f(X >= 60 & X <= 180) = 1-4*(sind(60)).^(2);
plot(X, S_f)

Upvotes: 2

rlbond
rlbond

Reputation: 67839

Your question is a bit unclear, but I think what you need to do is modify your independent variable when calculating S_f.

X=0:0.001:180;

S_f=1-4*(sind(min(X,60))).^(2);

plot(X,S_f)

You can see I used min(X,60) instead of X here, but you can change that as desired.

Upvotes: 2

Related Questions