MATLAB function printing extra matrix?

In summary, the Euler method can approximate y given x0 and y0, but when you use the function to output the zeroes, it produces an extra matrix.
  • #1
Gengar
13
0
I'm a newbie to all things programming, and have to write a program implementing the Euler method to approximate an ODE.

Anyways, the program works but when I use the function I wrote I get an extra matrix of zeroes above the desired output, which I don't understand as I have suppressed the output of the zero matrix with a semi-colon - the matrix is Eulerpoints. Here's the source code:


function [Eulerpoints] = Eulermethod(x0,y0,xn,steps)
%EULERMETHOD - this function will approximate y by the Euler method.
% EULERMETHOD(x0,y0,xn,steps) will return a list of points that
% approximate y in the interval [x0,xn] given our initial co-ordinate
% (x0,y0) and the number of steps.

Eulerpoints=zeros(steps+1,2);
start = 2;
finish = steps+1;
h=(xn-x0)/steps;
Eulerpoints(1,1) = x0, Eulerpoints(1,2) = y0;
fprintf(' n xn Yn\n 0 %2g %2g\n',x0,y0)

for I= start:finish
Eulerpoints(I,1) = x0 +h*(I-1);
Eulerpoints(I,2) = Eulerpoints(I-1,2) + h*(-2*Eulerpoints(I-1,2) - 0.25*Eulerpoints(I-1,1)*Eulerpoints(I-1,1) + 0.125); %% This line contains the ODE, which can be adjusted for a different equation.
fprintf('%3g %4g %6g\n',I-1,Eulerpoints(I,1),Eulerpoints(I,2))
end
%
end
 
Physics news on Phys.org
  • #2
The x0 in Line 11 needs to have a semi-colon after it. This will then return the Eulerpoints matrix as a variable and the [n xn yn] print out that you call for, but you will no longer have the output you were concerned with.

Here is the fixed code (using the CODE wrap that we have here on PF):

Code:
function [Eulerpoints] = Eulermethod(x0,y0,xn,steps)
%EULERMETHOD - this function will approximate y by the Euler method.
% EULERMETHOD(x0,y0,xn,steps) will return a list of points that
% approximate y in the interval [x0,xn] given our initial co-ordinate
% (x0,y0) and the number of steps.

Eulerpoints=zeros(steps+1,2);
start = 2;
finish = steps+1;
h=(xn-x0)/steps;
Eulerpoints(1,1) = x0; Eulerpoints(1,2) = y0;
fprintf(' n xn Yn\n 0 %2g %2g\n',x0,y0)

for I= start:finish
Eulerpoints(I,1) = x0 +h*(I-1);
Eulerpoints(I,2) = Eulerpoints(I-1,2) + h*(-2*Eulerpoints(I-1,2) - 0.25*Eulerpoints(I-1,1)*Eulerpoints(I-1,1) + 0.125); %% This line contains the ODE, which can be adjusted for a different equation.
fprintf('%3g %4g %6g\n',I-1,Eulerpoints(I,1),Eulerpoints(I,2))
end
%
end

Hope this helps.

Ben
 
  • #3
Ah thanks, yeh that's been bugging me (geddit?) for ages.
Cheers
 

What is a MATLAB function?

A MATLAB function is a set of instructions written in the MATLAB programming language that performs a specific task or calculation. It can take in input arguments, manipulate data, and output results.

How do I print an extra matrix in a MATLAB function?

To print an extra matrix in a MATLAB function, you can use the "disp" function to display the matrix in the command window. You can also use the "fprintf" function to print the matrix to a file.

What is the purpose of printing an extra matrix in a MATLAB function?

Printing an extra matrix in a MATLAB function can be useful for debugging purposes, as it allows you to see the values of variables and matrices at various points in the function. It can also be used to display intermediate results or final output.

How do I control the formatting of the printed matrix in a MATLAB function?

You can use formatting options with the "disp" and "fprintf" functions to control the appearance of the printed matrix. For example, you can specify the number of decimal places, the alignment, and the overall layout of the printed matrix.

Can I print multiple matrices in a single MATLAB function?

Yes, you can print multiple matrices in a single MATLAB function by using the "disp" or "fprintf" functions multiple times, each with a different matrix as the input argument. You can also use the "fprintf" function to print multiple matrices to a file.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
993
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
823
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
121
Back
Top