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:
A problem in plotting a function !!!

Subject: A problem in plotting a function !!!

From: Animesh Pandey

Date: 3 Mar, 2011 19:22:08

Message: 1 of 5

syms an bn a0;
syms t n;
syms x;
T = input('Enter the Time Period');
N = input('Enter the number of terms');
x = input('Enter the function');
f = @(t) x;

an = int(f(t).*cos(n*pi*t/T),t,-T,T)*(1/T);
bn = int(f(t).*sin(n*pi*t/T),t,-T,T)*(1/T);
a0 = int(f(t),t,-T,T)*(1/T);
y = a0;

pretty(a0);
pretty(an);
pretty(bn);


for i=-N:1:N;
     y = y + subs(an, i) + subs(bn, i);
end


This is a code to plot the Fourier series of any user defined function, using Symbolic Mathematics Toolbox (syms) !
The only problem I am facing is in plotting it !
I need to store every value of y in a matrix, and then use 'plot' function
OR
may be I could use 'ezplot' ! But I have not been ableto use them effectively.
Please Help !!!
Thnx !

Subject: A problem in plotting a function !!!

From: Paulo Silva

Date: 3 Mar, 2011 19:39:21

Message: 2 of 5

"Animesh Pandey" wrote in message <ikopov$qoj$1@fred.mathworks.com>...
> syms an bn a0;
> syms t n;
> syms x;
> T = input('Enter the Time Period');
> N = input('Enter the number of terms');
> x = input('Enter the function');
> f = @(t) x;
>
> an = int(f(t).*cos(n*pi*t/T),t,-T,T)*(1/T);
> bn = int(f(t).*sin(n*pi*t/T),t,-T,T)*(1/T);
> a0 = int(f(t),t,-T,T)*(1/T);
> y = a0;
>
> pretty(a0);
> pretty(an);
> pretty(bn);
>
>
> for i=-N:1:N;
> y = y + subs(an, i) + subs(bn, i);
> end
>
>
> This is a code to plot the Fourier series of any user defined function, using Symbolic Mathematics Toolbox (syms) !
> The only problem I am facing is in plotting it !
> I need to store every value of y in a matrix, and then use 'plot' function
> OR
> may be I could use 'ezplot' ! But I have not been ableto use them effectively.
> Please Help !!!
> Thnx !

Please give one example of the use of the script, T ? N ? x ?

Subject: A problem in plotting a function !!!

From: Paulo Silva

Date: 3 Mar, 2011 23:28:05

Message: 3 of 5

"Animesh Pandey" wrote in message <ikopov$qoj$1@fred.mathworks.com>...
> syms an bn a0;
> syms t n;
> syms x;
> T = input('Enter the Time Period');
> N = input('Enter the number of terms');
> x = input('Enter the function');
> f = @(t) x;
>
> an = int(f(t).*cos(n*pi*t/T),t,-T,T)*(1/T);
> bn = int(f(t).*sin(n*pi*t/T),t,-T,T)*(1/T);
> a0 = int(f(t),t,-T,T)*(1/T);
> y = a0;
>
> pretty(a0);
> pretty(an);
> pretty(bn);
>
>
> for i=-N:1:N;
> y = y + subs(an, i) + subs(bn, i);
> end
>
>
> This is a code to plot the Fourier series of any user defined function, using Symbolic Mathematics Toolbox (syms) !
> The only problem I am facing is in plotting it !
> I need to store every value of y in a matrix, and then use 'plot' function
> OR
> may be I could use 'ezplot' ! But I have not been ableto use them effectively.
> Please Help !!!
> Thnx !

I tried without much success to plot y, I used the functions vectorize and str2fun, try them.

Subject: A problem in plotting a function !!!

From: Think two, count blue.

Date: 3 Mar, 2011 23:52:59

Message: 4 of 5

On 11-03-03 05:28 PM, Paulo Silva wrote:
> "Animesh Pandey" wrote in message <ikopov$qoj$1@fred.mathworks.com>...
>> syms an bn a0;
>> syms t n;
>> syms x;
>> T = input('Enter the Time Period');
>> N = input('Enter the number of terms');
>> x = input('Enter the function');
>> f = @(t) x;
>>
>> an = int(f(t).*cos(n*pi*t/T),t,-T,T)*(1/T); bn =
>> int(f(t).*sin(n*pi*t/T),t,-T,T)*(1/T); a0 = int(f(t),t,-T,T)*(1/T); y = a0;

f(t) is x, and n is symbolic, so the integral of "an" will be an expression in
two variables; likewise for "bn"
>>
>> pretty(a0);
>> pretty(an);
>> pretty(bn);
>>
>>
>> for i=-N:1:N;
>> y = y + subs(an, i) + subs(bn, i);
>> end

As "an" and "bn" have two variables, "x" and "n", which variable are you
expecting that subs() will substitute the value of "i" in to? The answer is
that it will use "the default symbolic variable", symvar(an,1), which is "the
symbolic variable whose name is alphabetically closest to 'x'". As you have an
'x', it will substitute in for that, leaving "n" unchanged. Is that what you
want, or do you want to be substituting in for "n" ?

Warning: if you leave it substituting for x, then since x appears as a plain
multiplier term in the integral and you are running from -T to +T, each
negative multiple is going to cancel out a positive multiple, leaving the
result as 0.

Subject: A problem in plotting a function !!!

From: Think two, count blue.

Date: 4 Mar, 2011 00:20:34

Message: 5 of 5

On 11-03-03 05:52 PM, Think two, count blue. wrote:

> Warning: if you leave it substituting for x, then since x appears as a plain
> multiplier term in the integral and you are running from -T to +T, each
> negative multiple is going to cancel out a positive multiple, leaving the
> result as 0.

Small correction: that should say "from -N to +N"

Tags for this Thread

No tags are associated with 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