finding magnitude of a vector (2024)

14 views (last 30 days)

Show older comments

Ashlianne Sharma on 7 Nov 2020

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector

Commented: Ashlianne Sharma on 8 Nov 2020

Open in MATLAB Online

Hi all, I am trying to make a function and the first step within my function is to find the magnitude of a vector that I have named on another script. How do I do this? I willo put my code below to explain more.

FROM FUNCTION PAGE

function[a, e, nu, i, O, m] = hw_COE_sharmaAshlianne(R, V)

% Inputs

% R = Radius vector [km]

% V = Velocity vector [km/s]

%

% Outputs

% a = semi major axis [km]

% e = eccentricity [no units]

% nu = true anomoly [degrees]

% i = inclination [degrees]

% O = right ascention of the ascending node(RAAN) [degrees]

% w = argument of perigee [degrees]

% Known values

m = 398600; % [km^3/s^2]

% Step one find the magnitude of the radius and velocity vectors

r = norm(R);

v = norm(V);

FROM SCRIPT

% Create a structure for our function to find COE parameters

Rvector = [15370 1400 21950]; % [km]

Vvector = [-0.1 3.84 -0.2]; % [km/s]

% Call function with the structured variables we created above

[a, e, nu, i, O, m] = hw_COE_sharmaAshlianne(Rvector, Vvector);

so to reiterate my question, how do I find the magnitude of Rvector that is defined in my script on my function page (STEP ONE IN FUNCTION)

16 Comments

Show 14 older commentsHide 14 older comments

VBBV on 7 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117465

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117465

Edited: Walter Roberson on 8 Nov 2020

Open in MATLAB Online

>> hw_COE_sharmaAshlianne([...],[...])

Go to the command window and enter the vectors as shown above. Alternately run the script file in which the you are calling the function hw_COE_sharmaAshlianne(Rvector,Vvector)

Ashlianne Sharma on 7 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117525

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117525

Open in MATLAB Online

I have done that, I made the separate script by which I call my function with

[a, e, nu, i, RAAN, w] = hw_COE_sharmaAshlianne(Rvector, Vvector);

just as I stated in my original question.

Again, the question I asked is how do I code to find the magnitude of the vector because as I put above, it does not work. I would appreciate if you help me with the question I asked??

r = norm(R);

v = norm(V);

I tried putting the lines

r = Rvector;

but that does not work.

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117585

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117585

Edited: Walter Roberson on 8 Nov 2020

Open in MATLAB Online

Since norm calculates the magnitude of the vector and is defined inside the function as

function[a,e,nu,i,O,m]=

w_COE_sharmaAshlianne(Rvector,Vvector)

...

r = norm(Rvector);

v = norm(Vvector)

...

end % close function file with end statement

Close function file with end

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117675

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117675

Okay that makes a lot of sense! I am still running into an issue here at these lines of code. The error message I am getting it that I don't have enough input arguments (regarding my Rvector and Vvector).

In order for me to define the Rvector and the Vvector on my script, how do I get those values onto my function page?

I don't know if I am making sense haha, I guess what I am trying to say is how do I call a function with specific values that will run through my function page if I dont explicitely define the R and V vectors on my function page?

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117700

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117700

Edited: VBBV on 8 Nov 2020

In function page just define the same variable vectors as defined in script file as shown in my message. Inside the function you don't require or have to define R and V again. Instead you pass the vectors Rvector and Vvector as arguments and use them with norm

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117710

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117710

If you delete the semicolon at the

norm(Rvector) norm(Vvector)

You can see the values of magnitude in command window once the run is completed .

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117725

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117725

Okay, so you had an amzing explaination for the norm aspect, thank you so much.

This question seems simple, But I struggle with this since the start of the class.

Script %note I changed Rvector to R for simplicity

% Define given vectors as variables

R = [15370 100 21950]; % [km]

V = [-0.1 3.84 -0.2]; % [km/s]

% Call function with the structured variables we created above

[a, e, nu, i, RAAN, w] = hw_COE_sharmaAshlianne(R, V);

Function: line one, after this line, I finished the rest of my code.

function[a, e, nu, i, RAAN, w] = hw_COE_sharmaAshlianne(R, V)

Question:

When I was getting your help, I just defined my vectors inside my function. Like you say above, you dont need to do this. I took out my defined vectors from my function and I now get an error message saying I do not have enough input arguments. I don't know how to pass the variables through. Like I know the variables have to be the same on the script and function page, But that isnt working for me. Any explaination on passing variables through the function would be great help... The matlab version online hasnt been too much of help. Thank you Vasishta!

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117745

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117745

Did you name the file exactly the same as function name? I.e. the name of the file which contains your function code and name of the function must be same. The name of file in which function code is present must be

hw_COE_sharmaAshlianne.

If you name something else then it will not run.

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117750

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117750

yeah I saved the file as the same name as my function. Everything was working until I removed the R and V vectors from the function file

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117755

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117755

So when I run my script that I call my function on, it works perfectly, however, I when I run my function file, i get the message that there are not enough input arguments

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117760

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117760

Oh. You seem to have changed the names of variables Rvector and Vvector in your script file from which you're calling function. Change it to

Rvector = [values] Vvector = [values]

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117770

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117770

Edited: VBBV on 8 Nov 2020

Open in MATLAB Online

Keep the same structure like before in the first post.

% if true

% code

% end

Rvector = [15370 1400 21950]; % [km]

Vvector = [-0.1 3.84 -0.2]; % [km/s]

%

[a, e, nu, i, O, m] = hw_COE_sharmaAshlianne(Rvector, Vvector);

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117775

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117775

Rvector, Vvector must have same names in script file and function file

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117790

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117790

okay, that makes sense. Just so I am understanding,

script

Rvector = [15370 1400 21950]; % [km]

Vvector = [-0.1 3.84 -0.2]; % [km/s]

%

[a, e, nu, i, O, m] = hw_COE_sharmaAshlianne(Rvector, Vvector);

Function

function[a, e, nu, i, RAAN, w] = hw_COE_sharmaAshlianne(Rvector, Vvector)

and everywhere in my code that R and V exist in my function, the variable should be Rvector and Vvector aswell?

VBBV on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117800

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117800

Edited: VBBV on 8 Nov 2020

Open in MATLAB Online

https://in.mathworks.com/help/matlab/ref/function.html

Check the syntax of function declaration. You require space between the word function and output variable vector

%if true

% code

%end

function [a,e,nu,i,O,m]=

w_COE_sharmaAshlianne(Rvector,Vvector)

...

r = norm(Rvector);

v = norm(Vvector)

...

end % close function file with end statement

Ashlianne Sharma on 8 Nov 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117815

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/639990-finding-magnitude-of-a-vector#comment_1117815

Okay, Thank you so much for your help!! i will definitely look more to the page you sent me, I think that may be very useful. Again, Thank you so much for taking So SO much time out to help me. I am just a struggling college student haha but yes, I really appreciate your time and help Vasishta!

Sign in to comment.

Sign in to answer this question.

Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing

Find more on Matrix Indexing in Help Center and File Exchange

Tags

  • magnitude of vector
  • norm(r)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


finding magnitude of a vector (18)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

finding magnitude of a vector (2024)

FAQs

How can you find the magnitude of a vector? ›

Thus, the formula to determine the magnitude of a vector (in two-dimensional space) v = (x, y) is: |v| =√(x2 + y2). This formula is derived from the Pythagorean theorem.

How do you find the magnitude of a vector with 3 components? ›

Answer: The magnitude of a 3-dimensional vector with 3 components V = (a, b, c) is given as √(a2 + b2 + c2). Let's look into the given steps. Explanation: The magnitude of a vector signifies the positive length of a vector.

How to find the magnitude of a triangle? ›

So, if we can work out the lengths of the other two sides of the triangle, which we've labeled as 𝑎 and 𝑏, then Pythagoras's theorem tells us that for this triangle the magnitude of 𝐅 is equal to the square root of 𝑎 squared plus 𝑏 squared.

What is the formula for the magnitude of a vector product? ›

The magnitude vector product of two given vectors can be found by taking the product of the magnitudes of the vectors times the sine of the angle between them.

How to find the magnitude and direction of a resultant vector? ›

Finding Magnitude and Direction. To work with a vector, we need to be able to find its magnitude and its direction. We find its magnitude using the Pythagorean Theorem or the distance formula, and we find its direction using the inverse tangent function.

What is the equation for magnitude in physics? ›

This allows you to find the magnitude of a vector in any dimension. For a 2d vector the formula is |z|=√x2+y2 | z | = x 2 + y 2 , where x and y are the x and y components of the vector respectively. This can be generalized to 3 dimensions through |z|=√x2+y2+z2 | z | = x 2 + y 2 + z 2 and so on.

What is magnitude in physics easy? ›

In physics, magnitude is defined simply as “distance or quantity.” It depicts the absolute or relative direction or size in which an object moves in the sense of motion. It is used to express the size or scope of something. In physics, magnitude generally refers to distance or quantity.

How to find magnitude and direction of a vector given component form? ›

Case 1: Given components of a vector, find the magnitude and direction of the vector. Use the following formulas in this case. Magnitude of the vector is | v | = v x 2 + v y 2 . To find direction of the vector, solve tan θ = v y v x for .

What are the components of a vector of magnitude? ›

The magnitude of a vector is the length of the vector. It is calculated using the Pythagorean theorem: magnitude = sqrt(x^2 + y^2), where x and y are the x-component and y-component of the vector, respectively.

How to find the magnitude of a vector pq? ›

The magnitude of a vector P Q → is the distance between the initial point and the end point . In symbols the magnitude of P Q → is written as | P Q → | . If the coordinates of the initial point and the end point of a vector is given, the Distance Formula can be used to find its magnitude.

How do I get the magnitude of a vector? ›

How to Find Magnitude of a Vector?
  1. Step 1: Identify the x, y, and z components of the vector.
  2. Step 2: Find the square of all the x, y, and z components.
  3. Step 3: Add all the squares found in Step 2.
  4. Step 4: Find the square root of the sum obtained in Step 3.
May 14, 2024

What happens when you multiply two vectors? ›

When two vectors are multiplied with each other and the multiplication is also a vector quantity, then the resultant vector is called the cross product of two vectors or the vector product. The resultant vector is perpendicular to the plane containing the two given vectors.

How to normalize a vector? ›

In other words, to normalize a vector, simply divide each component by its magnitude. This is pretty intuitive. Say a vector is of length 5. Well, 5 divided by 5 is 1.

What is the magnitude of the vector 3i 4j? ›

Therefore the magnitude of the vector 3i +4j, is 5 units. This statement can be mathematically written as: |3i+4j| = 5. (The vertical lines mean magnitude).

What is the formula of magnitude of unit vector? ›

Unit Vector is represented by the symbol '^', which is called a cap or hat, such as ^a a ^ . It is given by ^a a ^ = a/|a| Where |a| is for norm or magnitude of vector a. It can be calculated using a unit vector formula or by using a calculator.

How to find the magnitude of resultant force? ›

Two forces that act in opposite directions produce a resultant force. If all the forces are balanced, the resultant force is zero. that is smaller than either individual force. To find the resultant force subtract the magnitude of the smaller force from the magnitude of the larger force.

How to find the magnitude of the sum of two vectors? ›

This is the addition of vectors formula: Given two vectors a = (a1, a2) and b = (b1, b2), then the vector sum is, M = (a1 + b1, a2 + b2) = (Mx, My). In this case, magnitude of the resultant vector sum M = |M| = √ ((Mx)2+(My)2) and.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5261

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.