Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MATLAB inconsistency

Subject: MATLAB inconsistency

From: Mohammad

Date: 9 Aug, 2010 18:30:20

Message: 1 of 7

Dear reader,

I have a sinusoidal function, v(x), which should be odd, since sin is an odd function, and x is an odd function, so sin(...)-x should be odd.
However when you plot it


Star=[0.592606202481246,-0.238596363969608,0.110220888524900,-0.048828233710702,0.019375152829649,-0.006571603729450,0.001814612417602,-3.812550640549345e-04,5.414560164816563e-05,-3.901808006180736e-06];
v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x;
fplot(v,[n o])


it is not odd. Why?

Thanks!

Cordially, Mohammad

Subject: MATLAB inconsistency

From: Walter Roberson

Date: 9 Aug, 2010 18:34:44

Message: 2 of 7

Mohammad wrote:

> I have a sinusoidal function, v(x), which should be odd, since sin is an
> odd function, and x is an odd function, so sin(...)-x should be odd.
> However when you plot it
>
>
> Star=[0.592606202481246,-0.238596363969608,0.110220888524900,-0.048828233710702,0.019375152829649,-0.006571603729450,0.001814612417602,-3.812550640549345e-04,5.414560164816563e-05,-3.901808006180736e-06];
>
> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x;
> fplot(v,[n o])
>
>
> it is not odd. Why?

Undefined variable n .

Subject: MATLAB inconsistency

From: Mohammad

Date: 9 Aug, 2010 19:00:16

Message: 3 of 7

z = 1;
n = -.5;
o = .5;
a = 0;
r = 10;
b = 1;
mat=zeros(r,r+1);
for i= 1:r
    for j = 1:r
        mat(i,j) = sin(j*pi*(o-(o-n)*(i-1)/2/r));%(o-(o-n)*(i-1)/2/r)=x
    end
    for j = 1+r
        mat(i,j) = z*((o-(o-n)*(i-1)/2/r))^b;
    end
end
for j=2:r,
     for i=j:r,
        mat(i,:) = mat(i,:) - mat(j-1,:)*mat(i,j-1)/mat(j-1,j-1);
     end
end
Bat = rot90(mat,2);
Hat=[Bat Bat(:,1)];
Hat(:,1)=[];
for j=2:r,
     for i=j:r,
        Hat(i,:) = Hat(i,:) - Hat(j-1,:)*Hat(i,j-1)/Hat(j-1,j-1);
     end
end
Fat = rot90(Hat,2);
Rat=[Fat Fat(:,1)];
Rat(:,1)=[];
Star=zeros(1,r);
for i = 1:r
    Star(1,i)=Rat(i,end)/Rat(i,i);
end
v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x^b;
fplot(v,[n o])
fid = fopen('hope.txt', 'w');
number3 = a;
fprintf(fid,['A0 is ' num2str(number3)]);
for i = 1:r
    number = Star(1,i);
    number2 = i;
    fprintf(fid,['\nA' num2str(number2) ' is ' num2str(number)]);
end
fclose(fid);
type hope.txt

Subject: MATLAB inconsistency

From: Walter Roberson

Date: 9 Aug, 2010 21:41:32

Message: 4 of 7

Mohammad wrote:

> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x^b;

Good question, but in the above expression you need x.^b rather than x^b .

If x is already a column vector, then you do not need the (:) after x
and if x is a row vector then you need - (x.').^b to avoid mismatches on the
sizes. If x is a 2D matrix rather than a vector then you are going to get
mismatches on the sizes.

> fplot(v,[n o])

I manually plotted v(-0.5:.1:0.5) and it _does_ appear to be an odd vector
like you expect. For some reason, though, fplot does not plot the same values
and does not plot the right hand tail at all correctly -- at least not in 2008b.

Subject: MATLAB inconsistency

From: Roger Stafford

Date: 10 Aug, 2010 03:10:06

Message: 5 of 7

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3phfs$p51$1@fred.mathworks.com>...
> Dear reader,
>
> I have a sinusoidal function, v(x), which should be odd, since sin is an odd function, and x is an odd function, so sin(...)-x should be odd.
> However when you plot it
>
> Star=[0.592606202481246,-0.238596363969608,0.110220888524900,-0.048828233710702,0.019375152829649,-0.006571603729450,0.001814612417602,-3.812550640549345e-04,5.414560164816563e-05,-3.901808006180736e-06];
> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x;
> fplot(v,[n o])
>
> it is not odd. Why?
> Thanks!
> Cordially, Mohammad

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3pj80$hea$1@fred.mathworks.com>...
> z = 1;
> n = -.5;
> o = .5;
> a = 0;
> r = 10;
> b = 1;
> mat=zeros(r,r+1);
> for i= 1:r
> for j = 1:r
> mat(i,j) = sin(j*pi*(o-(o-n)*(i-1)/2/r));%(o-(o-n)*(i-1)/2/r)=x
> end
> for j = 1+r
> mat(i,j) = z*((o-(o-n)*(i-1)/2/r))^b;
> end
> end
> for j=2:r,
> for i=j:r,
> mat(i,:) = mat(i,:) - mat(j-1,:)*mat(i,j-1)/mat(j-1,j-1);
> end
> end
> Bat = rot90(mat,2);
> Hat=[Bat Bat(:,1)];
> Hat(:,1)=[];
> for j=2:r,
> for i=j:r,
> Hat(i,:) = Hat(i,:) - Hat(j-1,:)*Hat(i,j-1)/Hat(j-1,j-1);
> end
> end
> Fat = rot90(Hat,2);
> Rat=[Fat Fat(:,1)];
> Rat(:,1)=[];
> Star=zeros(1,r);
> for i = 1:r
> Star(1,i)=Rat(i,end)/Rat(i,i);
> end
> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x^b;
> fplot(v,[n o])
> fid = fopen('hope.txt', 'w');
> number3 = a;
> fprintf(fid,['A0 is ' num2str(number3)]);
> for i = 1:r
> number = Star(1,i);
> number2 = i;
> fprintf(fid,['\nA' num2str(number2) ' is ' num2str(number)]);
> end
> fclose(fid);
> type hope.txt
- - - - - - - - - -
  Hello Mohammad. I think I know what went wrong with fplot. It apparently started from the 'n' end at x = -.5 and initially spaced x points close together. However during the long stretch in the middle where your v values are very nearly a constant zero, it got overconfident and began to space the x values farther and farther apart until near the other 'o' end at x = +.5 the last five x values out of a total of 121 it tried were:

 x = -.208
 x = -.144
 x = -.016
 x = +.240
 x = +.500

This huge gap misses all the interesting variation in v at the right end and accounts for why the plot is so terribly inaccurate. It is the kind of behavior that functions like quad do in their calls to integrand functions which remain nearly constant for too long an interval.

  My advice would be not to use fplot at all on the kind of stuff you're working with. Just use the straight 'plot' function. That way you know exactly what spacing is being used in the plotting. It is not being left up to some algorithm which is trying to be too smart for its own good.

Roger Stafford

Subject: MATLAB inconsistency

From: Mohammad

Date: 10 Aug, 2010 17:53:04

Message: 6 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3qfue$d4q$1@fred.mathworks.com>...

> x = -.208
> x = -.144
> x = -.016
> x = +.240
> x = +.500
>
> This huge gap misses all the interesting variation in v at the right end and accounts for why the plot is so terribly inaccurate. It is the kind of behavior that functions like quad do in their calls to integrand functions which remain nearly constant for too long an interval.
>
> My advice would be not to use fplot at all on the kind of stuff you're working with. Just use the straight 'plot' function. That way you know exactly what spacing is being used in the plotting. It is not being left up to some algorithm which is trying to be too smart for its own good.
>
> Roger Stafford

How would I use fplot in this situation (I'm kinda new to this...)

Subject: MATLAB inconsistency

From: Roger Stafford

Date: 10 Aug, 2010 18:25:07

Message: 7 of 7

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3s3m0$i5c$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3qfue$d4q$1@fred.mathworks.com>...
> > .......
> > My advice would be not to use fplot at all on the kind of stuff you're working with. Just use the straight 'plot' function. That way you know exactly what spacing is being used in the plotting. It is not being left up to some algorithm which is trying to be too smart for its own good.
> >
> > Roger Stafford
>
> How would I use fplot in this situation (I'm kinda new to this...)
- - - - - - - - -
  As I have said, you should stay strictly away from 'fplot' with your problem! Perhaps you meant ask how you could use 'plot'?

 x = linspace(n,o,500).'; % Choose the number of points you want to plot
 v = sin(pi*x*(1:r))*Star.'+a-x.^b;
 plot(x,v,'y.') % Or plot(x,v,'y-')

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us