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:
Why is my program execution slow ???

Subject: Why is my program execution slow ???

From: Animesh Pandey

Date: 1 Mar, 2011 12:55:18

Message: 1 of 3

syms an bn a0;
syms t n;
syms x;
y = sym(0);
T = input('Enter the Time Period');
N = input('Enter the number of terms');

f = @(t) heaviside(t+0.5)-heaviside(t-0.5);


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);

hold on
for i=1:0.01:N;
    y = y + subs(an, i) + subs(bn, i);
    ezplot(i, y), grid on
end
hold off

Thus program computes the Fourier series of a square wave.
But it gives the plot like after 4 - 5 minutes. Also the plot is not perfect.
Please help !!!!
Thanx ! :)

Subject: Why is my program execution slow ???

From: Franck Dernoncourt

Date: 1 Mar, 2011 15:07:20

Message: 2 of 3

"Animesh Pandey" wrote in message <ikiqbm$qpd$1@fred.mathworks.com>...
> syms an bn a0;
> syms t n;
> syms x;
> y = sym(0);
> T = input('Enter the Time Period');
> N = input('Enter the number of terms');
>
> f = @(t) heaviside(t+0.5)-heaviside(t-0.5);
>
>
> 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);
>
> hold on
> for i=1:0.01:N;
> y = y + subs(an, i) + subs(bn, i);
> ezplot(i, y), grid on
> end
> hold off
>
> Thus program computes the Fourier series of a square wave.
> But it gives the plot like after 4 - 5 minutes. Also the plot is not perfect.
> Please help !!!!
> Thanx ! :)

I wanted to give it a try but I'm getting errors with your code, are you sure you're using ezplot the right way? Besides what values of T and N did you choose?

??? Error using ==> inlineeval at 15
Error in inline expression ==> 
 Error: The input character is not valid in MATLAB statements or
 expressions.

Error in ==> inline.feval at 36
        INLINE_OUT_ = inlineeval(INLINE_INPUTS_,
        INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in ==> ezplotfeval at 52
    z = feval(f,x(1));

Error in ==> ezplot>ezparam at 387
            X = ezplotfeval(x, T);

Error in ==> ezplot at 165
        hp = ezparam(cax, f{1}, f{2}, vars, labels, args{2:end});

Error in ==> sym.ezplot at 50
      h = ezplot(char(f),char(y));

Error in ==> test at 23
    ezplot(i, y);

Subject: Why is my program execution slow ???

From: Animesh Pandey

Date: 1 Mar, 2011 16:00:19

Message: 3 of 3

> I wanted to give it a try but I'm getting errors with your code, are you sure you're using ezplot the right way? Besides what values of T and N did you choose?

I did not get any error.
The only problem that I was facing was delay in execution, though I am not getting a good plot !!! :(
There is some problem in the for loop !
I chose N as 23
and T (time period) as 2.

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