MATLAB function printing extra matrix?

Click For Summary
SUMMARY

The forum discussion addresses an issue with a MATLAB function that implements the Euler method for approximating ordinary differential equations (ODEs). The problem involved an extra matrix of zeroes appearing above the desired output, which was resolved by adding a semi-colon after the assignment of the initial value x0 in the code. The corrected code eliminates the unwanted output while maintaining the intended functionality of returning the Eulerpoints matrix and printing the results of the approximation.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with numerical methods, specifically the Euler method
  • Knowledge of ordinary differential equations (ODEs)
  • Experience with MATLAB's matrix operations and output formatting
NEXT STEPS
  • Learn about MATLAB's debugging techniques to identify and fix common coding errors
  • Explore advanced numerical methods for solving ODEs, such as Runge-Kutta methods
  • Investigate MATLAB's built-in functions for matrix manipulation and output control
  • Study the impact of step size on the accuracy of numerical approximations in ODEs
USEFUL FOR

Beginner MATLAB programmers, students learning numerical methods, and anyone working with ordinary differential equations who seeks to improve their coding practices and output management in MATLAB.

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 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K