"Arie Driga" <a_driga@yahoo.com> wrote in message <i85tns$aks$1@fred.mathworks.com>...
> "AJP " <adam.noakes07@imperial.ac.uk> wrote in message <i858u8$e2n$1@fred.mathworks.com>...
>
> > I warned you to not to copy and paste my code and to check it for typos before using it  this is the sort of debugging you should do yourself. The error message "undefined function func1" is letting you know that the function called has not been defined.
>
> Yes you are right, it was my bad.
> I do have doubt now, I have fixed the method using trapz. It works now, but the answer between trapz method and quad method is differ by order of magnitude.
>
> Here is for trapz:
> %=================================================
> %integration
>
> stp = ic/int_icr;
> x = zeros((length(t)1),(int_icr));
> y1 = zeros((length(t)1),(int_icr));
> y2 = zeros((length(t)1),(int_icr));
> int1 = zeros(1,(length(t)1));
> int2 = zeros(1,(length(t)1));
>
> for i=1:(length(t)1)
> for j=1:(int_icr+1)
> x(i,j) = t(1,i)+(j1)*stp;
> y1(i,j) = exp(a*x(i,j)d*cos(omg*x(i,j)));
> y2(i,j) = exp(a*x(i,j)d*cos(omg*x(i,j))) * sin(omg*x(i,j));
> end
> end
>
> for i=1:size(x,1)
> int1(1,i) = trapz(x(i,:),y1(i,:));
> int2(1,i) = trapz(x(i,:),y2(i,:));
> end
>
>
> int1_sum = zeros(1,length(int1));
> int2_sum = zeros(1,length(int1));
> for i=1:length(int1)
> int1_sum(1,i) = sum(int1(1:i));
> int2_sum(1,i) = sum(int2(1:i));
> end
>
> int1_new=[0 int1_sum];
> int2_new=[0 int2_sum];
> %==============================================
>
> omg for trapz is the same as w in quad:
>
> And here is the quad method based on your suggestion:
>
> %==============================================
>
> %integration
> function y1=funct1(t,a,d,w)
> y1 = a*t  d*cos(w*t);
> end
>
> function y2=funct2(y1,t,d,w)
> y2 = y1  d.*cos(w*t);
> end
>
> intgr1=zeros(1,length(t));
> intgr2=zeros(1,length(t));
> for i=2:length(t)
> intgr1(i) = quad(@(t)funct1(t,a,d,w),0,t(i));
> intgr2(i) = quad(@(t)funct2(t,a,d,w),0,t(i));
> end
==============================================
OK, I have some general observations, hopefully these will help you.
(1) The functions you have in your trapz method and those in the quad method are totally different.
In the trapz method you have writteN:
> y1(i,j) = exp(a*x(i,j)d*cos(omg*x(i,j)));
> y2(i,j) = exp(a*x(i,j)d*cos(omg*x(i,j))) * sin(omg*x(i,j));
Whereas in the functions called by quad are:
> y1 = a*t  d*cos(w*t);
> y2 = y1  d.*cos(w*t);
Notice there is no exp() in y1 or y2 here, so of course the results will be different. You really must pay attention to what you are writing!
Check "doc quad" at the command line.
(2) Whenever you use trapz, you must always multiply by the step length. You have forgotten to do this, so the trapz function has assumed your step length is 1. This probably explains why the results for quad and trapz are very different.
Look at "doc trapz"
As an example, say I want to find the integtral of sin(x) between 0 and pi. If you do this by hand you will find the real answer is exactly 2.
To do this in MATLAB, I write the following code at the command line or in an .m file:
%====================================
format long % this makes MATLAB display all numbers to their fulll number of places
x=linspace(0,pi,756); % note 756 is just some arbitrary number of elements I chose, it could be any number
dx=x(2)x(1) % gives the step length
y=sin(x);
intgr=trapz(y)*dx % (=1.99998)
%====================================
If you forget to put " *dx " after "trapz" then the result is 218.99, clearly wrong.
(3) quad is much preferable to trapz in this case as it is more accurate and you don't need to generate any vectors for your y1, y2 and x as in your trapz method.
(4) I said before that you have to save your function files func1 and func2 in separate files, and then a separate script file that calls quad and your own functions. The code you put above suggests you have them all in the same file, which is not right. This may also be why your results are unexpected.
(5) I don't understand why you need the double for loop in your trapz method  if you are only looping over the values in a row vector then you only need a single loop, passing over each element in the row in turn. It is likely this method is either generating incorrect results or is simply inefficient, creating values that arene't needed.
(5) Using loops to generate new vectors or matrices, as in your trapz method, is less efficient than using elementwise operators like .* ./ .^
These operators allow you to perform operations on each element of a matrix or vector.
For example, say I have two 2x2 matrices, x=[1,2;3,4] and y=[4,5;6,7]. If I type x*y, I get ans=[16,19;36,43]. This is like a normal matrix multiplication.
However, if I write x.*y, I get ans=[4,10;18,28] which is like I have multiplied each element in x with the corresponding element in y.
This is much much more efficient than using a loop like this:
for n=1:numel(x)
ans(n)=x(n)*y(n)
end
Search online for "matllab element wise operations" or "matlab vectorized operations" for more info.
