MATLAB-output values matrix

  • MATLAB
  • Thread starter Bostonpancake0
  • Start date
  • Tags
    Matlab Matrix
In summary, the conversation discusses the process of writing a code for the recursive trapezoidal rule, which is meant to find the integral of a function within a given interval with a specified tolerance. The code is designed to stop when the difference between the next and previous values falls below 10^-4 after a certain number of intervals have been sub-divided. One suggestion is to use a while loop to compare the values and store them in a matrix. Other tips and considerations for the code are also mentioned.
  • #1
Bostonpancake0
42
0
Hi,

I'm trying to write a code for the recursive trapezoidal rule. The code must have the bounds of integration and tolerance. The code is meant to stop when the tolerance between the next and previous value is below 10^-4, given n number of times to sub split the intervals.

My progress on the code is below;

function [s1] = trapzd(func,a,b,toler)
%
% function s1 = trapzd(func,a,b,s,n)
%
% This routine uses the trapezoidal
%rule to evaluate
% the integral between a and b of the
%integrand defined
% by the function func. n is the number
%of times the interval is subdivided.
%
% The new approximation (s1) is
%adjusted from the previous
%approximation
% (s) from a prior call.
%

s=0

for n=1:10

it=2.^(n-2);
tnm=it;
h=(b-a)./tnm;
x=a+0.5.*h;
sum=0.0;


for j=1:it
sum=sum+feval(func,x);
x=x+h;
end
s=0.5.*(s+h.*sum);

s1 = s

end


end

The code seems to be working fine but I'm wondering if there is anyway to put the out put values for s1 into a matrix immediately after one loop. What I am hoping to achieve is, is to put a while loop around all other for loops stating that the code will stop until s1(n+1)-s1(n) is less than 10^-4. Its probably an easy answer to this question but I just cannot remember how to do it.

Also if there is any other tips that can be given will be much appreciated.


Thanks
 
Physics news on Phys.org
  • #2
I'm wondering if there is anyway to put the out put values for s1 into a matrix immediately after one loop.

Can you be more specific? Are you saying you think your code can be written with 1 loop rather than two, or that you don't like the way s1 is defined?

What I am hoping to achieve is, is to put a while loop around all other for loops stating that the code will stop until s1(n+1)-s1(n) is less than 10^-4. Its probably an easy answer to this question but I just cannot remember how to do it.

You'll have to store the s1(n) value to later compare it to the s1(n+1) value. Something like this should work, but I haven't tested it much:

Code:
s=0;
ds=1;

while ds > toler
    for n=1:10
        sN = s;
        it=2.^(n-2);
        tnm=it;
        h=(b-a)./tnm;
        x=a+0.5.*h;
        sum=0.0;
        
        for j=1:it
            sum=sum+feval(func,x);
            x=x+h;
        end
        s=0.5.*(s+h.*sum);
        
        s1 = s;
        ds = abs(s1-sN);
    end
end

You might also wish to add some maximum number of iterations to an implementation like this, to ensure it doesn't get stuck trying to reach an unachievable accuracy. Just add a ticker to count the iterations and error out when it goes above some threshold.
 

1. What is a MATLAB-output values matrix?

A MATLAB-output values matrix is a two-dimensional array that contains the results of a mathematical calculation or operation performed using MATLAB software. It is a common way to store and organize data in MATLAB, and can be manipulated and analyzed using various built-in functions and commands.

2. How do I create a MATLAB-output values matrix?

To create a MATLAB-output values matrix, you can use the "matrix" function or simply enter the values directly into the command window using square brackets. For example, to create a 3x3 matrix with values 1, 2, 3 in the first row and 4, 5, 6 in the second row, you can enter [1 2 3; 4 5 6] in the command window.

3. Can I perform calculations on a MATLAB-output values matrix?

Yes, you can perform various mathematical operations on a MATLAB-output values matrix using built-in functions and operators. For example, you can use the "mean" function to calculate the average of all the values in the matrix, or use the "*" operator to multiply the matrix by a scalar value.

4. How can I access specific values in a MATLAB-output values matrix?

You can access specific values in a MATLAB-output values matrix by using row and column indices. For example, to access the value in the second row and third column of a matrix, you can use the syntax matrixName(2,3). You can also use logical indexing, which allows you to specify certain conditions for which values to access.

5. Can I convert a MATLAB-output values matrix into a different data type?

Yes, you can convert a MATLAB-output values matrix into a different data type using the "double", "single", or "int8" functions, among others. These functions allow you to convert the matrix into a specific data type, such as a double-precision floating-point number or an 8-bit signed integer.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
123
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
17K
Back
Top