Series Evaluation and Function Plotting in Octave/Matlab

How to evaluate a series using a recurrence relation in Octave/Matlab?

Explain the consequences of each coding statement for plotting a function using Octave code.

Series Evaluation in Octave/Matlab

(a) To evaluate the given series in Octave/Matlab, we can use a while loop to keep adding terms until the condition un < 10^-8 is met. Here's the code:

u = 0.5; % initial value of u

n = 0; % initial value of n

un = u; % initial value of un

while un >= 10^-8

  n = n + 1; % increment n

  un_1 = un - 1 + n * n; % calculate un+1 using the recurrence relation

  un_1 = un; % update un-1 for the next iteration

  un = un_1; % update un for the next iteration

end

Function Plotting in Octave/Matlab

(b) The given Octave code plots a function using the meshgrid and mesh functions. Let's discuss the consequences of each coding statement:

  1. (x, y) = meshgrid(-3:0.3:3):
    - This line creates a grid of x and y values ranging from -3 to 3 with a step size of 0.3.
    - The resulting x and y matrices will have dimensions (21 x 21).
  2. z = x .* exp(-x.^2 - y.^2):
    - This line calculates the values of z using element-wise operations on the matrices x and y.
  3. subplot(2,2,1) mesh(z), title(subplot(2,2,1)):
    - This line creates a subplot grid of 2 rows and 2 columns and selects the first subplot for plotting.
  4. subplot(2,2,2) mesh(z) view(-37.5,70), title(subplot(2,2,2)):
    - This line selects the second subplot and sets its viewing angle to (-37.5, 70).
  5. subplot(2,2,3) mesh(z) view(37.5,-10), title(subplot(2,2,3)):
    - This line selects the third subplot and sets its viewing angle to (37.5, -10).
  6. subplot(2,2,4) mesh(z) view(0,0), title(subplot(2,2,4)):
    - This line selects the fourth subplot and sets its viewing angle to (0, 0).

The given Octave/Matlab code evaluates a series using a recurrence relation and generates 3D mesh plots of a function. The series evaluation code stops when the value of un falls below 10^-8.

(a) The series evaluation in Octave/Matlab involves updating the values of un and un-1 based on the given recurrence relation until the condition un < 10^-8 is met. The variable n keeps track of the index of the term in the series.

(b) For function plotting, the Octave code utilizes meshgrid to create a grid of x and y values, calculates the corresponding z values using element-wise operations, and then creates 3D mesh plots with different viewing angles in subplots.

Each coding statement in the function plotting section has specific consequences related to creating the mesh plots, setting viewing angles, and adding titles to the subplots. Understanding these statements is essential for visualizing the function effectively in Octave/Matlab.

← Application of hazen williams equation calculating unit headloss Exploring tokenstats function counting integers and real numbers →