Efficient Output Organization for MATLAB Three-Point Derivative Approximation

In summary, the conversation was about a code for approximating a derivative using a three-point formula. The variables used were n for the number of iterations, x for the input value, i for iteration number, approx for the approximation of the derivative, and error for the difference between the actual and approximate derivative. The issue discussed was how to put the output values in a clean matrix with 30 rows and 4 columns. The solution was to use the function [A] = ApproxSinDeriv(x,n) and create a matrix with zeros and then fill it with the desired values using a for loop. The code was successfully implemented and the desired output was achieved.
  • #1
Providence88
7
0
So I have this code:

Code:
function ApproxSinDeriv(x,n)
h=.5;
for i = 0:n
    h;
    approx = (1/(2*h))*(sin(x+h)-sin(x-h));
    error = abs(approx-cos(x));
    h = h/2;
    A=[i,h,approx,error]
end

Basically, it's a three-point formula for approximating a derivative, where the variables n = the number of iterations, x = the input value for the sin function, i = iteration number, approx=the approximation of the derivative, and error = the difference between the actual derivative and the approximation.

I put A equal to a matrix of these values, but the output comes out in 30 clunky, individual, one-row matrices.

How would I go about putting this data in an array? That is, one nice clean matrix with 30 rows and 4 columns [i,h,approx,error]?

Thanks!

-Eric.
 
Last edited:
Physics news on Phys.org
  • #2
Code:
function [A] = ApproxSinDeriv(x,n)
A = zeros(n+1,4)
h=.5;
for i = 0:n
    approx = (1/(2*h))*(sin(x+h)-sin(x-h));
    error = abs(approx-cos(x));
    h = h/2;
    A(i+1,:)=[i,h,approx,error];
end
 
  • #3
Thank you! That worked great!
 

1. How do I save my MATLAB output as a file?

To save your MATLAB output as a file, you can use the "save" function. First, create a variable to store your output. Then, use the save function and specify the filename and file format. For example:
myOutput = [1, 2, 3];
save('output.txt', 'myOutput', '-ascii');

This will save the output as a text file with the name "output.txt".

2. How do I display my output in a specific format?

To display your output in a specific format, you can use the "fprintf" function. This function allows you to specify the format and the variables you want to display. For example:
myOutput = 5.678;
fprintf('The output is: %.2f', myOutput);

This will display the output as "The output is: 5.68".

3. How can I organize my output into a table?

To organize your output into a table, you can use the "table" function. This function allows you to create a table with different variables as columns. For example:
var1 = [1, 2, 3];
var2 = ['a', 'b', 'c'];
myTable = table(var1, var2);

This will create a table with two columns, one for "var1" and one for "var2".

4. Can I customize the appearance of my output?

Yes, you can customize the appearance of your output by using formatting commands such as "disp", "fprintf", and "sprintf". These commands allow you to add text, numbers, and special characters to your output to make it more visually appealing. You can also use HTML tags or MATLAB's "format" function to change the display format of your output.

5. How do I suppress the display of my output in MATLAB?

To suppress the display of your output in MATLAB, you can add a semicolon at the end of your statement. This will prevent the output from being displayed in the command window. For example:
myOutput = 10;
disp(myOutput);

This will display the output as "10" in the command window. However, if you add a semicolon like this:
myOutput = 10;
disp(myOutput);

The output will not be displayed in the command window.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
936
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
802
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
Replies
12
Views
178
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
Back
Top