Explore BrainMass

# Coding in Matlab - Dot, cross, and triple products

Not what you're looking for? Search our solutions OR ask your own Custom question.

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

See the attached file.

The objective is to write MATLAB codes that calculate scalar, vector and triple (scalar) products of vectors.

Example: the scalar product function is R^3

Copy and paste the following programme into the MATLAB editor and save the program as scalar_prod.m

function z = scalar_prod(x,y) % clears all the variables from memory
z = 0; % initiate z
for i = 1:3; % loop
z = z+x(i)*y(i); % calculate each product and add it to the previous sum
end; % end the loop

To see an example of the use of the programme, return to the command window and type:
>> x = [1; 2; 3];
>> y = [-1; 1; 1];
>> scalar_prod(x; y)

>> ans = 4

1. Write a function to calculate the scalar product of two vectors in R^4. Test your new function with the vectors x = [3;4;2;1] and y = [-2;3;4;5].

2. Write a function for calculating the cross product of two vectors in R^3. Test this function with the vectors x = [6;3;8] and y = [-3;2;-7].

3. Write a function for calculating the triple (scalar) product of three vectors in R^3. Test this function with the vectors x = [1;2;3], y = [-1;8;9] and z = [-2;3;4]. What is the MATLAB function, which allows a direct computation of a triple product?

https://brainmass.com/math/discrete-math/coding-matlab-dot-cross-triple-products-524121

## SOLUTION This solution is FREE courtesy of BrainMass!

1. To find the scaler product of two vectors in R^4, we know each vector has exactly 4 entries. The Matlab code to extract the i-th entry for a vector x is x(i). So in your file scaler_prod.m, write:

function p=scaler_prod(x,y)
p=x(1)*y(1)+x(2)*y(2)+x(3)*y(3)+x(4)*y(4);

2. Now using the cross product formula of two vectors in R^3, in the file cross_prod.m, write:

function c=cross_prod(x,y)
c=zeros(3,1);
c(1)=x(2)*y(3)-y(3)*x(2);
c(2)=x(3)*y(1)-x(1)*y(3);
c(3)=x(1)*y(2)-x(2)*y(1);

Here, zeros function initialize the vector c to a colum vector with all zero entries. We then apply the formula for cross product at each entry.

3. By definition, the triple scaler product of x,y,z is the dot product of x with the cross product of y,z. There are different ways to solve this in matlab.

The first choice is to use the cross product function you just defined. Unfortunately, the scaler product we defined is for R^4, so we cannot use it here. So in triple_prod.m, write:

function tri = triple_prod(x,y,z)
p = scaler_prod(x, cross_prod(y,z));
tri = x(1)*p(1)+x(2)*p(2)+x(3)*p(3);

Here p is the cross product of y,z, and we then dot product p with x to get the final answer.

Another way to define triple product is through determinant, the triple product is the determinant of a three by three matrix whose columns are x,y,z. Hence, assuming the input vectors are column vectors, we may define the scaler triple product as

function tri = triple_prod(x,y,z)
tri = det([x,y,z]);

Here, [x,y,z] forms a matrix with columns x,y,z.

Which one you choose really depends on your definition in the lecture.

In matlab, scaler product is defined as "dot", the cross product is defined as "cross". Hence, to compute the triple product directly in matlab is:

dot(x,cross(y,z));.

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!