1

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?

pad
  • 41,040
  • 7
  • 92
  • 166
J Martin
  • 13
  • 4
  • This basically sounds like a duplicate of these other questions: [How can I create a piecewise inline function in MATLAB?](http://stackoverflow.com/q/796072/52738), [MATLAB Piecewise Functions + Vector Manipulation](http://stackoverflow.com/q/1549888/52738), [How do I perform statements on the dependent variable of a graph in matlab?](http://stackoverflow.com/q/4357995/52738) – gnovice May 23 '11 at 15:16

3 Answers3

5

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])
volting
  • 16,773
  • 7
  • 36
  • 54
1

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)')
volting
  • 16,773
  • 7
  • 36
  • 54
J Martin
  • 26
  • 1
0

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 );
volting
  • 16,773
  • 7
  • 36
  • 54
Soul Reaver
  • 2,012
  • 3
  • 37
  • 52