MATLAB fprintf(): Create Table of Height & Distances Earth & Mars

  • Context: MATLAB 
  • Thread starter Thread starter HAL10000
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

This discussion focuses on using MATLAB's fprintf() function to create a formatted table displaying heights and distances to the horizon for Earth and Mars. The user has defined a height vector h ranging from 0 to 10,000 feet and a distance calculation function dist_func() that computes distances based on planetary radii. The challenge lies in printing the height vector alongside the calculated distance matrix using fprintf(), specifically requiring the use of a for loop to format the output correctly. The provided code demonstrates the setup for this task, including the necessary calculations and matrix manipulations.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of matrix operations in MATLAB
  • Knowledge of the fprintf() function for formatted output
  • Basic concepts of distance calculations related to planetary radii
NEXT STEPS
  • Research MATLAB fprintf() formatting options for multi-column output
  • Learn about MATLAB meshgrid() function for creating grids from vectors
  • Explore the use of for loops in MATLAB for iterating through matrix elements
  • Investigate distance calculation formulas for planetary bodies
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly students and educators working on homework assignments involving formatted output and distance calculations for planetary science.

HAL10000
Messages
21
Reaction score
0
I'm trying to create a table with three separate columns using fprintf(). The first column consists of a height vector h = 0:500:10000 and the second and third columns are part of the matrix Distance.
I've tried looking online and I can't seem to find a way to make the vector and the matrix to print side by side. There are easier ways to obtain this result but the homework problem is telling me to use fprintf(). Please help.

This is the code for the function:
Code:
%--------------------------------------------------------
function [ Distance ] = dist_func( h , r )
% dist_func This function calculates the distance to the horizon
%from various heights on different planets.
Distance = sqrt((2.*r.*h) + h.^2);
end
%--------------------------------------------------------
... and the program:
Code:
%--------------------------------------------------------
% Homework Problem 1 This program calculates the distance
%to the horizon from different heights on Earth and Mars.
%
% Define the height matrix values in feet.
h = 0:500:10000;
%
% Define the radius values for Earth and Mars in feet.
radius_earth = 3963;
radius_mars = 2108.5;
%
% Define radius matrix in feet
r = [radius_earth;radius_mars];
%
% Create grid containing all height and radius values in feet.
[hg,rg] = meshgrid(r,h);
% Calculate distance for both radii and convert to miles.
Distance = dist_func(hg,rg)*(1/5280);
% Place data into table format
disp('  Height   Distance on Earth    Distance on Mars')
fprintf('%8.2f\n',h); fprintf('%8.2f %8.2f\n',Distance')
The last line is an attempt at making the table.
 
Last edited by a moderator:
Physics news on Phys.org
Use a for loop to print the matrix elements. For each print statement, separate the matrix entries by tab, \t.
 

Similar threads

Replies
5
Views
9K
Replies
5
Views
8K
  • · Replies 43 ·
2
Replies
43
Views
8K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
1
Views
13K
Replies
3
Views
4K