Asked by mohannad issa
on 17 May 2018

hello all if any one can tell me whats the problem in this code every time i run it illegal use of reserved keyword elseif every time i run it.

tau=input('enter approximate pulse width:'); type_wave=input('Enter waveform type: 1 =decaying exponential; 2=sinewave:'); if type_wave== 1 t_max=input('Enter duration of exponential:'); elseif type_wave==2 t_max=1; end clf a=[]; t=0:.01:t_max; cn=tau; n_max=floor(t_max/tau) for n=1:n_max LL=(n-1) * tau; UL=n* tau; If type_wave==1 a(n)=(1/cn)*quad(@integrand_exp,LL,UL); elseif type_wave==2 a(n)=(1/cn)* quad(@integrand_sine,LL,UL); end end disp('') disp('c n') disp (cn) disp(' ') disp('Expansion coefficients(approximated pulses not normalised:') disp(a) disp (' ') x_approx=zeros(size(t)); for n=1:n_max x_approx=x_approx + a(n)*phi(t/tau,n-1); end if type_wave==1 MSE=quad(@integrand_exp2,0,t_max)- cn*a*a'; elseif type_wave==2 MSE =quad(@integrand_sine2,0,t_max) - cn*a*a'; end disp('Mean-squared error:') disp(MSE) disp(' ') if type_wave==1 plot(t,x_approx),axis([0 t_max -inf inf]),xlabel('t'),ylabel('x(t);x_a_p _p_ r_ o_ x(t)') hold plot(t,exp(-t)) title(['approximation of exp(-t) over [0,',num2str(t_max),'] with contigious rectangular pulses of width',num2str(tau)]) elseif type_wave==2 plot(t,x_approx),axis([0 t_max -inf inf]),xlabel('t'),ylabel('x(t); x_a_p_p_r_o_x(t)') hold plot(t,sin(2*pi*t)) title(['Approximation of sin(2*pi*t) over [0,',num2str(t_max),'] with contigious rectangular pulses of width',num2str(tau)]) end

function z =integrand_exp(t) z=exp(-t);

function z =integrand_sine(t) z=sin(2*pi*t);

function z= integrand_exp2(t) z=exp(-2*t);

function z = integrand_sine2(t) z=(sin(2*pi*t)).^2;

enter approximate pulse width:0.125

Enter waveform type: 1 =decaying exponential; 2=sinewave:2

n_max =

8

Answer by Walter Roberson
on 17 May 2018

"If" is not a valid MATLAB keyword.

Corrected version attached.

mohannad issa
on 17 May 2018

Walter Roberson
on 17 May 2018

I do not get that far. My runs stop at the line

x_approx=x_approx + a(n)*phi(t/tau,n-1);

because phi is not defined.

Image Analyst
on 17 May 2018

You need to define integrand_sine2 - it's not a built-in function as far as I can see.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Rena Berman (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401285-illegal-use-of-reserved-keyword-elseif-every-time-i-run-it#comment_569414

(Answers Dev) Restored edit

## John Kelly (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401285-illegal-use-of-reserved-keyword-elseif-every-time-i-run-it#comment_569447

Please do not delete your question after obtaining an Answer from the MATLAB Community. The Community Members spent a lot of time and effort on answering your question. This question and answer will be beneficial in the future to other Community Members who have a similar problem

Sign in to comment.