简体   繁体   English

MATLAB中的函数绘图

[英]Function plotting in MATLAB

I have the following function: 我有以下功能:

f(t) = 0                  if    t < 0     
f(t) = 2*t^2 - 4*t +3     if   1 <= t < 2   
f(t) = Cos(t)             if    2 <= t

I am a new MATLAB user, and I do not how to plot the function on a single figure over the range 0<=t<=5. 我是MATLAB的新用户,我不打算在0 <= t <= 5范围内的单个图形上绘制函数。

Any ideas about What I have to do? 关于我必须做什么的任何想法?

Write a function for your Laplace formula. 为您的Laplace公式编写一个函数

Something like this 像这样

function [ft] = func(t)
    if t <= 0
        ft = 0;
    elseif t > 0 &&  t < 2
        ft = 2 * t^2 - 4 * t + 3;
    elseif t >= 2
        ft = cos(t);
    end    

You can then plot the function with fplot , the second parameter defines the plotting range. 然后可以使用fplot绘制函数,第二个参数定义绘制范围。

fplot('func', [0, 5])

thanks for your help but I could not implement any code or commands to get the answer. 感谢您的帮助,但我无法实现任何代码或命令来获取答案。 Instead of, I was lucky and I found an example and the MATLAB commands are as follow: 相反,我很幸运,我找到了一个示例,MATLAB命令如下:

x=linspace(0,5,3000);
y=(0*x).*(x<1) + (2*(x.^2)-(4.*x)+3).*((1<=x) & (x<2))
+ (cos(x)).*(2<=x);
plot(x,y, '.'), grid
axis([0 5 -2 4])
title ('Plot of f(t)'), xlabel('t'), ylabel('f(t)')

If you mean limiting x axis, then after using plot use 如果要限制x轴,则在使用绘图后使用

xlim([xmin xmax])

In your case 就你而言

xlim([0 5])

Use ylim for limiting y axis 使用ylim限制y轴


Ok, I think I misunderstood you 好吧,我想我误会了你

Also I think, you've made mistake in your formulas 我也认为,您在公式中犯了错误

f(t) = 0 if 0<= t < 1 f(t) = 2*t^2 - 4*t +3 if 1 <= t < 2 f(t) = Cos(t) if 2 <= t 如果0 <= t <1 f(t)= 2 * t ^ 2-4 * t +3,则f(t)= 0如果1 <= t <2 f(t)= Cos(t)如果2 <= t

figure;
hold on;
x = 0:0.1:0.9;  y = 0 * x;                      plot( x, y );
x = 1:0.1:1.9;  y = 2 * x * x - 4 * x + 3;      plot( x, y );
x = 2:0.1:5;    y = cos( x );                   plot( x, y );

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

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