简体   繁体   English

如何将 plot 分段 function (MATLAB)?

[英]How to plot piecewise function (MATLAB)?

I have the following code, which plots a line that crosses the origin.我有以下代码,它绘制了一条穿过原点的线。 I need to plot a horizontal line at a given constant until I cross the line.我需要 plot 一条给定常量的水平线,直到我越过这条线。

For example: y = 0.31 until the constant crosses the line y= 7.51e-5*x.例如:y = 0.31 直到常数穿过直线 y= 7.51e-5*x。 How do I go about doing this?我 go 如何做到这一点?

 %%// Create a function handle of your function
 f = @(x,a) 7.51619693312e-5*x;
 %%// Plot the data
 x = linspace(0, 15000);
 as = 9.5;
 plot(x, bsxfun(f,x(:),as));
 xlabel(gca, '$W_{to}/S_w$ (Pa)', ...
'Interpreter', 'latex', ...
'FontName', 'Times New Roman', ...
'FontSize', 14, ...
'FontWeight', 'normal', ...
'FontAngle', 'normal')
 ylabel(gca, {'$T_{to}/W_{to}$'}, ...
'Interpreter', 'latex', ...
'FontName', 'Times New Roman', ...
'FontSize', 14, ...
'FontWeight', 'normal', ...
'FontAngle', 'normal')

How about solving for the point of intersection and plotting the function as two parts?如何求解交点并将 function 分成两部分?

m = 7.51e-5; %some slope 
y1 = 0.31; %Some y value
x_intercept = y1/slope; %Compute the part were the piecing takes place.
x1 = 0; %Left x-limit (Choose some start value)
x2 = x_intercept*1.2; %right x-limit (Choose some end value)
y2 = m*x2;
ax = axes; %Axes object in which to plot.
hold(ax,'on'); %Tell ax to remember all plots.
p1 = plot([x1,x_intercept],[y1,y1]); %Plots the constant part.
p2 = plot([x_intercept,x2],[y1,y2]); %Plots the slant part.
%Add labels and customize...

Also, for plotting purposes, there is no need to evaluate the equation of a line over several discrete points.此外,出于绘图目的,无需计算多个离散点上的直线方程。 Just evaluate the end points and the plot function will plot a straight line between the end points.只需评估端点和 plot function 将 plot 端点之间的一条直线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM