MATLAB MATLAB function printing extra matrix?

AI Thread Summary
The discussion addresses a programming issue with a MATLAB function that implements the Euler method for approximating an ODE. A user encountered an unexpected output of an extra matrix of zeroes, despite using a semicolon to suppress output. The solution involves adding a semicolon after the assignment of x0 in the code, which resolves the issue and allows for the desired output without the extra matrix. The corrected code is provided, ensuring that the Eulerpoints matrix is returned correctly. This adjustment eliminates the confusion and improves the function's output clarity.
Gengar
Messages
13
Reaction score
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
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
 
Ah thanks, yeh that's been bugging me (geddit?) for ages.
Cheers
 

Similar threads

Replies
2
Views
3K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
8
Views
2K
Replies
4
Views
2K
Replies
9
Views
2K
Back
Top