Plot piecewise function with couple of kink points

조회 수: 4 (최근 30일)
Jorge Martinez Compains
Jorge Martinez Compains 2022년 3월 15일
댓글: Voss 2022년 3월 22일
Thank you so much beforehand if anyone could help me with this. I have used Matlab in the past. However, I am a bit overwhelmed trying to plot this piecewise function.
I need to plot a piece wise function (q) where the parameters are i2, r, sigma1, sigma2, sigma3, v and beta and the variable is i1. The function is:
q= 1 {if i1=(1+r)(1-sigma3/(sigma3+v*sigma2))+sigma3/(sigma3+v*sigma2)*(i2-v*sigma2)}
q=1-(i1-1/beta)/sigma1 {if i1>=sigma1*((i2-1/beta)/v*sigma2 -1) -1/beta}
q=1-(i1-r-1)/sigma1*(1-(sigma3/sigma1)*(1+sigma3/sigma1+sigma3/(sigma2*v))**(-1))+(sigma3/sigma1)*(1+sigma3/sigma1+sigma3/(sigma2*v))**(-1)*((i2-r-1)/v*sigma2-1) {if if i1<sigma1*((i2-1/beta)/v*sigma2 -1) -1/beta}
q=1-(i1-1/beta)/(sigma1+sigma3) {if sigma2=0 or v=0}
q=0 {if sigma1=0}
The values for the parameters is:
r=(1-beta)/beta
beta=0.9
i2=4
sigma1=0.1
sigma2=0.2
sigma3=0.7
v=0.5
I would greatly appreciate if anyone could show me how to do it for an array of values for i1 that has a range from 0 to 10 separated by 0.1.

채택된 답변

Voss
Voss 2022년 3월 15일
You can use logical indexing for this:
beta=0.9;
r=(1-beta)/beta;
i2=4;
sigma1=0.1;
sigma2=0.2;
sigma3=0.7;
v=0.5;
i1 = 0:0.1:10;
q = NaN(size(i1));
if sigma1 == 0
q(:) = 0;
elseif sigma2 == 0 || v == 0
q(:) = 1-(i1-1/beta)/(sigma1+sigma3);
else
% idx is an array of logical type (i.e., each element is true or
% false) which is the same size as i1.
% each element of idx tells you whether or not the corresponding
% element of i1 satisfies the condition (i1 == (1+r)*...):
idx = i1 == (1+r)*(1-sigma3/(sigma3+v*sigma2))+sigma3/(sigma3+v*sigma2)*(i2-v*sigma2);
% now set the elements of q where idx is true (i.e., that condition is
% true) to 1:
q(idx) = 1;
% idx is another logical array, this time based on a different
% condition (i1 >= sigma1*...):
idx = i1 >= sigma1*((i2-1/beta)/v*sigma2 -1)-1/beta;
% set the elements of q where this second condition is true to
% the values calculated from the expression below.
% (note that i1(idx) is used - only using the elements of i1 where the
% condition is true)
q(idx) = 1-(i1(idx)-1/beta)/sigma1;
% final condition to check (i1 < sigma1*...)
idx = i1 < sigma1*((i2-1/beta)/v*sigma2 -1) -1/beta;
% set the corresponding elements of q, again based on i1(idx)
q(idx) = 1-(i1(idx)-r-1)/sigma1*(1-(sigma3/sigma1)*(1+sigma3/sigma1+sigma3/(sigma2*v))^(-1))+(sigma3/sigma1)*(1+sigma3/sigma1+sigma3/(sigma2*v))^(-1)*((i2-r-1)/v*sigma2-1);
end
plot(i1,q);
References:
https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html (scroll down to near the bottom for the logical indexing section)
  댓글 수: 2
Jorge Martinez Compains
Jorge Martinez Compains 2022년 3월 22일
Thanks a lot! That helped me a lot!
Voss
Voss 2022년 3월 22일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by