f



Need Help to solve Matlab problem Urgently

Dear All,

Anyone here can enlighten me where did i go wrong and how to amend my
mistake in the matlab scripts (main script & function file), in order
to get the plot out.

Currently, there is no plot because there is an warning stating as

Warning: Divide by zero.
(Type "warning off MATLAB:divideByZero" to suppress this warning.)
> In C:\MATLAB6p5\work\Assignment_Qn1new.m at line 44
Previous value q0 q1 q2 s

ans =

   NaN NaN NaN NaN

Latest value q0 q1 q2 s

ans =

   NaN NaN NaN NaN

My Matlab Main script are as follows:

clear
pack
clc
format long e
M = 1601; 	% Number of samples taken starting from nT=0
T = 0.025;	 	% in second
r = ones(1,M);		% Unit step signal
q0 = 0.009; % Guess q0 = 0.009
q1 = -q0;
q2 = 0;
initial_q = [q0 q1 q2];
s_old = []; %store the value of s
delta_q = 0.0000009; % Smaller than gamma
gamma = 0.00005; % step length
number_of_trial = 51;

for index = 1:1:number_of_trial,

	%Finding S
    [k,e] = ek_function(T,q0,q1,q2,r);
    s = sum(abs(e));
   
	%Finding S_q0
	[k,e] = ek_function(T,(q0+delta_q),q1,q2,r);
	s_q0 = sum(abs(e));

	%Finding S_q1
	[k,e] = ek_function(T,q0,(q1+delta_q),q2,r);
	s_q1 = sum(abs(e));

	%Finding S_q2
	[k,e] = ek_function(T,q0,q1,(q2+delta_q),r);
	s_q2 = sum(abs(e));

	grad_of_s_due_to_q0 = (s_q0-s)/delta_q;
	grad_of_s_due_to_q1 = (s_q1-s)/delta_q;
	grad_of_s_due_to_q2 = (s_q2-s)/delta_q;

	delta = sqrt(grad_of_s_due_to_q0^2+...
        	 grad_of_s_due_to_q1^2+...
	 grad_of_s_due_to_q2^2);

	constant_p = gamma/delta;

	q0_old = q0;
	q1_old = q1;
	q2_old = q2;
	if index < number_of_trial,
		s_old = s;
	end

	q0 = q0 - constant_p * grad_of_s_due_to_q0;
	q1 = q1 - constant_p * grad_of_s_due_to_q1;
	q2 = q2 - constant_p * grad_of_s_due_to_q2;

end

save try_step initial_q q0 q1 q2 T r index delta_q gamma

save try_step_backcopy initial_q q0_old q1_old q2_old s_old
save try_step_backcopy q0 q1 q2 s T r index delta_q gamma -append

disp('Previous value q0 q1 q2 s')
[q0_old,q1_old,q2_old,s_old]
disp('Latest value q0 q1 q2 s')
[q0,q1,q2,s]

[k,e] = ek_function(T,q0,q1,q2,r);
c = r - e;

figure('Name','Assignment Q1',...
		 'NumberTitle','off')

subplot(1,2,1),plot(k,e)
xlabel('time(nT) with T = 0.5s'),ylabel('e(nT)')
title('Error signal,e(nT)')

subplot(1,2,2),plot(k,c)
xlabel('time(nT) with T = 0.5s'),ylabel('c(nT)')
title('Output signal,c(nT)')

My Function file as follows:
% Error function program for calculating the error(s)
function [k,e]=ek_function(T,q0,q1,q2,r)
%Check the number of input arguments
if nargin < 5,
   error('The number of input arguments is less than 5.');
end

%Check the number of output arguments
if nargout < 2,
   error('The number of output arguments is less than 2.');
end

a = 5;
beta5 = q0 - [exp(-a*T)*q0];
beta6 = q1 - [exp(-a*T)*q1];
beta7 = q2 - [exp(-a*T)*q2];

[n,M] = size(r);

signal_r = r;		%Make a copy of signal r(nT)

r = [0 0 0];		%r(1) point to r(nT);
			%r(2) point to r(nT-T);
			%r(3) point to r(nT-2T);
		
e_temp = [0 0 0 0 0 0 0 0 0];	%e_temp(1) point to e(nT);
			%e_temp(2) point to e(nT-T);
			%e_temp(3) point to e(nT-2T);
			%e_temp(4) point to e(nT-3T);
            %e_temp(5) point to e(nT-4T);
            %e_temp(5) point to e(nT-5T);
            %e_temp(5) point to e(nT-6T);
            %e_temp(5) point to e(nT-7T);
            %e_temp(5) point to e(nT-8T);

for k = 1:1:M,
	r(1) = signal_r(k);
	e_temp(1) = r(1)-(1+exp(-a*T))*r(2)+exp(-a*T)*r(3)+...
	(1+exp(-a*T))*e_temp(2)-exp(-a*T)*e_temp(3)-...
    beta5*e_temp(7)-beta6*e_temp(8)-beta7*e_temp(9);
   
	e(k)=e_temp(1);
	r(4)=r(3);
	r(3)=r(2);
	r(2)=r(1);
    e_temp(5)=e_temp(4);
	e_temp(4)=e_temp(3);
	e_temp(3)=e_temp(2);
	e_temp(2)=e_temp(1);
end

k=T*[0:1:M-1];		% The time that a particular sample is taken

Kindly revert me asap, if you know how to resolve my p
0
4/26/2005 6:29:50 AM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
773 Views

Similar Articles

[PageSpeed] 49

Kent wrote:

> Warning: Divide by zero.
> (Type "warning off MATLAB:divideByZero" to suppress this warning.)
>> In C:\MATLAB6p5\work\Assignment_Qn1new.m at line 44

Hi,

I think that you don't need more help...

Well, Matlab gives you the answer... a division by 0 at line 44.

So here we are :

44) constant_p = gamma/delta;

What is the value of delta ?

So this is only a warning. I meant I'm not sure that this stops your
code as an error. Does it return any error ?

J�r�me
0
dutmatlab (3797)
4/26/2005 6:42:20 AM
Reply: