How to Correct Syntax for a Distance Calculation Method in MatLab?

  • Context: MATLAB 
  • Thread starter Thread starter Zoobie
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around correcting the syntax for a distance calculation method in a Matlab program designed to model the solar system. Participants explore issues related to function parameters and object properties within a class structure.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to create a "planet" class in Matlab, including properties for position and velocity, and a method to calculate distance to another planet object.
  • Another participant requests clarification on the command line used to call the function, noting a lack of definition for x, y, and z in the original function.
  • A participant shares the constructor method for the "planet" class, detailing how it initializes the object's properties.
  • After modifying the function to accept two parameters, one participant reports that the function now returns the expected outcome and questions whether "obj" refers to the calling object while "obj2" refers to the passed object.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the initial syntax issue, but there is agreement that the modified function works as intended. The understanding of object referencing in Matlab remains partially clarified.

Contextual Notes

There are unresolved questions regarding the proper referencing of object properties in Matlab, as well as the initial error related to function inputs.

Zoobie
Messages
3
Reaction score
0
I was tasked with creating a simple program to model the solar system using Matlab. One step in the way I wanted to do this was by making a "planet" class that stored the x,y,z position, and the x,y,z velocity.

One of the methods, then, would be something to that took in another "planet" object and computed the distance between them. This is easy enough to do, but I can't figure out what the syntax is.

I have:

function [dist] = distanceTo(obj)
dist = sqrt(abs(obj.x - x)^2 + abs(obj.y- y)^2 + abs(obj.z-z)^2);
end

My intention here is that obj will be an inputted planet, and typing obj.x will give you its x-value. This however isn't the case, and whenever I try to run it I get a "Too many inputs error"

Can anyone help me figure out the correct syntax? Thanks for any help and happy holidays
 
Physics news on Phys.org
tell us the command line or code line that you call the function with. Also, I see nowhere that your program is going to get x,y, and z from.
 
That's just the function code. There is also a constructor method:

function p = planet(x_pos,y_pos,z_pos,x_vel,y_vel,z_vel,mass)
p.x = x_pos;
p.y = y_pos;
p.z = z_pos;
p.xV = x_vel;
p.yV = y_vel;
p.zV = z_vel;
p.mass = mass;
end

So the command line I type in is:

p = planet(1,2,3,4,5,6,7); p.distanceTo(p); (Edit: I would hope this returned zero).

This returns a "Too many inputs error".

Thanks for any help

Edit: Also in regards to where it gets x,y, and z from - Those are properties in the class. I think I remember in java typing this.(variable name) to reference a variable in the class, but I'm not sure what the equivalent would be in Matlab.
 
I actually made some progress on it - I changed the parameters of the function to:

function [dist] = distanceTo(obj,obj2)
dist = sqrt(abs(obj.x-obj2.x)^2 + abs(obj.y - obj2.y)^2 + abs(obj.z + obj2.z)^2);
end

and typing in the same line returned the expected outcome. My question then turns to is "obj" the self referencing line, while "obj2" is the other "planet" object? From what I can tell this is how it works.

Thanks again
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K