From: Shaun on 19 Feb 2010 11:48 I am wanting to plot a piecewise continuous function, I am not able to find the correct syntax to do this. Here is a simple example that should give a triangle centered at x=0, f(x)= { 1+x -1<=x<=0 { 1-x 0<x<=1 This is how I am incorrectly trying to do this (it seems others suggest this approach over using the inline function) x=-1:0.01:1; f = @(x) ( (1+x).*(-1<=x<=0) + (1-x).*(0<x<=1) ); plot(x,f(x)) How can I fix this or is there a better approach?
From: James Allison on 19 Feb 2010 12:05 Very close. This works: f = @(x) ((1+x).*(and(-1<=x,x<=0)) + (1-x).*(and(0<x,x<=1))) fplot(f,[-1 1 0 1]) Note that f is undefined outside the interval [-1 1]. -James Shaun wrote: > I am wanting to plot a piecewise continuous function, I am not able to > find the correct syntax to do this. Here is a simple example that > should give a triangle centered at x=0, > > f(x)= { 1+x -1<=x<=0 > { 1-x 0<x<=1 > > This is how I am incorrectly trying to do this (it seems others suggest > this approach over using the inline function) > > x=-1:0.01:1; f = @(x) ( (1+x).*(-1<=x<=0) + (1-x).*(0<x<=1) ); > plot(x,f(x)) > > How can I fix this or is there a better approach?
From: Shaun on 19 Feb 2010 12:46 Thanks for the response. Is there any way to plot this using plot(x,f)? What I want to do is plot different piecewise continuous functions along with their approximations. So, how can I get the function f in the correct form for the plot command? Then I could compare the exact function f and the approximate function f_appx on the same plot: plot(x,f,x,f_appx).
From: James Allison on 19 Feb 2010 12:59 Sure: f = @(x) ((1+x).*(and(-1<=x,x<=0)) + (1-x).*(and(0<x,x<=1))); x=-1:0.01:1; plot(x,f(x)) -James Shaun wrote: > Thanks for the response. > > Is there any way to plot this using plot(x,f)? What I want to do is > plot different piecewise continuous functions along with their > approximations. So, how can I get the function f in the correct form > for the plot command? Then I could compare the exact function f and the > approximate function f_appx on the same plot: plot(x,f,x,f_appx).
From: Shaun on 19 Feb 2010 13:32 That works, I thought I already tried that, but I guess not. Thanks again for the quick help.
|
Next
|
Last
Pages: 1 2 Prev: Matlab complied llb and LabVIEW Next: Controlling different movements in simmechanics |