# Matlab help needed!

I need help getting started on a project for school. If someone could help me understand how to do find_d, build_Xvec, and especially the function calls for main.m. Go to http://www.ews.uiuc.edu/~cs101/ to see the full details. Any help at all would be very appreciated!!!

• In particular, what is your problem?

For example, for the function find_d.
The text says: [italic]It accepts 2 parameters "D" and "K" (in this order). It returns a row vector of the values of the distance between each balloon. [/italic]
So, you can write the first line of the function in this way:
[code]function d = find_d(D,K);
[/code]Then, the text also report the formula to compute the distances d:
[img=http://www.ews.uiuc.edu/~cs101ta/mps/mp1/image010.gif]
So, what is the problem?
• I don't know how to write that summation into a matlab function. Additionally, I don't know how to call a function into the main file of this project. I know this is elementary, but I'm just confused by it all.
• Perfect. It's not a problem if its elementary, but it is important to me to understand what is your problem so that I know how to answer.

so,
you have D that is a number, and K that is a vector.
for example D=10; and K=[2 3 2 4];

[code]for i=1:length(K)
d(i)=(D./K(i))/sum(1./K(:));
end[/code]

so, for i=1, you divide D for the first element of K, and divide this for the sum, in this example:1/2+1/3+1/4+1/5.
The function [b]sum[/b] simply returns the sum of the element of a vector.

to recall this function in the main you have to write in the main:
[code]d=find_d(D,K);[/code]
in this way you recall the function with D and K as inputs and d as output.
Obviously before this line you have to specify D and K.

I hope you understand what I mean, but if you have some questions, I will explain.

• That makes sense, thank you so much. I thought that is how you called a function, but wasn't sure.
My next question is regarding the function build_Xvec:

This function builds a row vector that contains the x-positions of the left anchor point, the balloons, and the right anchor point in the system. It accepts a single parameter "d". Using the MATLAB built-in function cumsum, calculate the x-positions of the balloons and the right anchor using d and store them in a vector called "sub_x". Then, you can build a new vector by appending the x-coordinate of the left anchor (which is 0) to the front of sub_x. Your function should return this new row vector. In "main.m", write a function call to "build_Xvec" and store the return value in a vector called X (capitalized).

The reason for building a vector that has the form of

[x_position_of_left_anchor_point x_positions_of_weigths x_position_of_right_anchor_point]

is that we need to plot the graph of the system in the latter step of the project. The above provides all the x-coordinates needed to plot the system.

function x = built_Xvec(d)

From here I don't know what to do.
• Well, it's a good start!
let's go on.

the function cumsum... have you search it in the matlab help?
the matlab help says:
[italic]B = cumsum(A) returns the cumulative
sum along different dimensions of an array.[/italic]
and make an example:
cumsum(1:5)
ans =
[1 3 6 10 15]
so, you give it a vector and it returns the first value of the vector, then the fist plus the second, etc.
[x1 x1+x2 x1+x2+x3 x1+x2+x3+x4 ...]
it's all right?

This is all you need because in this way you identify all the x-positions of the balloons, included the right anchor point. We need only the right anchor that is not included.

so, I write the flow of the code in words and you can try to write the matlab code, it's ok?

1. your first line is correct, so:
function x = built_Xvec(d)
2. inizialize the vector x with the correct length with all zeros (search in the help and use the function [b]zeros[/b]).
3. create a vector x1 using the function cumsum on d.
4. assign to the elements from the second to the last of x the content of x1.

In this way you "force" the first element to 0 and assign the others elements with the function cumsum.

Let's think and try to write your code. If there's anything you can't understand, let's ask.