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:
Summation

Subject: Summation

From: Mohammad

Date: 6 Aug, 2010 22:45:05

Message: 1 of 7

Dear reader,

I have a vector with r entries which are imputed previously. I want to PLOT a function v(x), such that v=@x Star(1,1)*sin(pi*x)+Star(1,2)*sin(2*pi*x)+Star(1,3)*sin(3*pi*x)+....Star(1,n)*sin(n*pi*x)+...Star(1,r-1)*sin((r-1)*pi*x)+Star(1,r)*sin(r*pi*x)

Is there a simple way to do this using matrix summation.

(P.S. I only need to plot it, so if there is a way to not need to create a function, I'll take it)

This is what I though of so far:
Yarr=ones(r,200)
for i = 1:r
for x = 1:200
Yarr(i,x)=Star(1,i)*sin(i*pi*x)
end
end

Subject: Summation

From: Matt J

Date: 7 Aug, 2010 16:59:04

Message: 2 of 7

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3i39h$44i$1@fred.mathworks.com>...
> Dear reader,
>
> I have a vector with r entries which are imputed previously. I want to PLOT a function v(x), such that v=@x Star(1,1)*sin(pi*x)+Star(1,2)*sin(2*pi*x)+Star(1,3)*sin(3*pi*x)+....Star(1,n)*sin(n*pi*x)+...Star(1,r-1)*sin((r-1)*pi*x)+Star(1,r)*sin(r*pi*x)
=================

v=@(x) sum( Star.*sin( pi*x(:)*(1:r) ) ,2 );
plot(1:200,v(1:200));

Subject: Summation

From: Matt J

Date: 7 Aug, 2010 17:17:04

Message: 3 of 7

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3k3co$ldr$1@fred.mathworks.com>...

>
> v=@(x) sum( bsxfun(@times, sin( pi*x(:)*(1:r) ) , 2 );
> plot(1:200,v(1:200));
==============

Sorry, make that

v=@(x) sum( bsxfun( @times, sin( pi*x(:)*(1:r) ),Star) , 2 ).';

plot(1:200,v(1:200));

Subject: Summation

From: Roger Stafford

Date: 7 Aug, 2010 17:45:04

Message: 4 of 7

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3k4eg$qp0$1@fred.mathworks.com>...
\> v=@(x) sum( bsxfun( @times, sin( pi*x(:)*(1:r) ),Star) , 2 ).';
- - - - - - - - - - -
  You don't need bsxfun for this one:

 v = @(x) sin(pi*x(:)*(1:r))*Star.';

Roger Stafford

Subject: Summation

From: Mohammad

Date: 7 Aug, 2010 18:54:03

Message: 5 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3k630$8dp$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3k4eg$qp0$1@fred.mathworks.com>...
> \> v=@(x) sum( bsxfun( @times, sin( pi*x(:)*(1:r) ),Star) , 2 ).';
> - - - - - - - - - - -
> You don't need bsxfun for this one:
>
> v = @(x) sin(pi*x(:)*(1:r))*Star.';
>
> Roger Stafford

Thanks everyone. Just for educational purpose (So I can do this in the future) does the x(:) mean that x is changing, and what does the .' after Star signify.

THANKS! this made my program a whole lot simpler.

Cordially, Mohammad

Subject: Summation

From: Matt Fig

Date: 7 Aug, 2010 20:48:04

Message: 6 of 7

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message
> Thanks everyone. Just for educational purpose (So I can do this in the future) does the x(:) mean that x is changing, and what does the .' after Star signify.
>
> THANKS! this made my program a whole lot simpler.
>
> Cordially, Mohammad

Look

doc colon
doc transpose

Subject: Summation

From: Roger Stafford

Date: 7 Aug, 2010 20:57:03

Message: 7 of 7

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3ka4b$cd0$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3k630$8dp$1@fred.mathworks.com>...
> > "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3k4eg$qp0$1@fred.mathworks.com>...
> > \> v=@(x) sum( bsxfun( @times, sin( pi*x(:)*(1:r) ),Star) , 2 ).';
> > - - - - - - - - - - -
> > You don't need bsxfun for this one:
> >
> > v = @(x) sin(pi*x(:)*(1:r))*Star.';
> >
> > Roger Stafford
>
> Thanks everyone. Just for educational purpose (So I can do this in the future) does the x(:) mean that x is changing, and what does the .' after Star signify.
>
> THANKS! this made my program a whole lot simpler.
>
> Cordially, Mohammad
- - - - - - - - -
  No, x(:) makes a column vector out of the elements of x just in case your x was a row vector. (Actually it will make a column vector out of a matrix too.) The .' after Star takes its transpose. This is necessary so that the number of columns in sin(pi*x(:)*(1:r)) will equal the number of rows in Star.' and yield a valid matrix multiplication with the * operation in between them. You should read up in your matlab manual about indexing and matrix multiplication. It's an important part of one's matlab education.

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