Plot piecewise function with couple of kink points
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!