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

  • MATLAB
  • Thread starter Zoobie
  • Start date
  • Tags
    Matlab
In summary, the conversation is about creating a program in Matlab to model the solar system using a "planet" class. The main issue is figuring out the correct syntax for the distanceTo method, which calculates the distance between two planets. The code for the method and constructor are provided, and the correct command line is given. Eventually, the correct syntax for the distanceTo method is determined to be using "obj" as the self-referencing line and "obj2" as the other planet object.
  • #1
Zoobie
3
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
 
  • #5
.Hi there,

Happy holidays to you too! It seems like you are on the right track with your method for calculating the distance between two planets. The syntax for accessing a property of an object in Matlab is "objectName.propertyName". So in your case, it would be "obj.x" to access the x position of the inputted planet.

However, the "Too many inputs error" suggests that you may be passing in more than one input to your method. Make sure that when you are calling the method, you are only passing in one planet object, like this: "distance = planet1.distanceTo(planet2)".

Also, just a small tip, you can simplify your distance calculation by using the built-in "norm" function in Matlab, like this: "dist = norm([obj.x-x, obj.y-y, obj.z-z])".

I hope this helps and good luck with your solar system modeling!
 

1. What is a method in Matlab?

A method in Matlab is a function or routine that is used to perform a specific task or computation. It is a reusable block of code that can be called multiple times within a program.

2. How do I create a method in Matlab?

To create a method in Matlab, you can use the syntax "function [output] = method_name(input1, input2, ...)", where [output] is the variable that the method will return and input1, input2, etc. are the variables that the method will take as input. Then, you can write the code for the method within the function block.

3. Can a method in Matlab have multiple outputs?

Yes, a method in Matlab can have multiple outputs. You can specify the number of outputs in the function declaration using square brackets, such as "function [output1, output2] = method_name(input1, input2, ...)".

4. How do I call a method in Matlab?

To call a method in Matlab, you can simply use the method name followed by parentheses and any necessary input variables. For example, if the method is called "calculate_average" and takes in two numbers as input, you can call it by typing "calculate_average(5, 10)".

5. Can I pass a method as an input to another method in Matlab?

Yes, you can pass a method as an input to another method in Matlab. This is known as a function handle. To do this, you can use the "@" symbol before the method name to create a handle, such as "@method_name", and then pass it as an input to the other method.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top