简体   繁体   中英

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.

Any ideas about What I have to do?

Write a function for your Laplace formula.

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('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:

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

xlim([xmin xmax])

In your case

xlim([0 5])

Use ylim for limiting y axis


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

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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