Hi guys,
I'm working with Matlab and I can't seem to get something right here. Basically I need to create a vector that starts at 0, and then goes to n (where n is a number I enter, which I can do easily). But on each one of the values in this large vector I need to run an equation against it, and then sum up all the values. If that makes sense. Here's what I've got:
>>n = [0:100];
This creates the vector from 0-> 100 as the values.
>> S = ((-1)^(i))*(1/(2*i+1));
'i' is supposed to be what each value of 'n' is. But I'm not sure how to do this. The book only explains that if you do something like: n.^4 it raises each value of 'n' to the power of 4. But I don't know how to take an equation and apply it to each value in the vector. I hope it makes more sense now.
Does anyone have any insight on how to do this?
e: QUESTION 2:
Any idea why my script:
function V = Vfuel(h)
r=0.6; %converted to meters to keep the units the same
SV = ((4/3)*(pi)*(r^3))/2; %sphere's total volume
disp(r)
disp(h)
disp(h+r)
When I enter 0.2 as my h, gives me:
>> number24 0.2
0.6000
0.2
48.6000 46.6000 50.6000
It's like it treats the r=0.6 as a vector.
QUESTION 3:
So I wrote a user function:
function V = Vfuel(h)
r = 0.6; %converted to meters to keep the units the same
SV = ((4/3)*(pi)*(r^3))/2; %sphere's total volume
if (r > h) %if it's just the sphere
V = SV - ((pi*(h^2)*((3*r) - h))/3);
elseif (r < h) %above the sphere
V = SV;
cylH = h-r;
if (cylH <= 1.4) %doesn't fill the entire cylinder
V = V + (pi*(r^2)*cylH);
elseif (cylH > 1.4)
V = V + (pi*(r^2)*cylH);
coneH = cylH - 1.4;
V = V + (1/3)*pi*(r^2)*coneH;
end
end
Now I need to run this against h, from 0 -> 2.8, and then plot it. But I can't seem to figure out how to use h against it. I defined h as: h = (0:0.1:2.8), and then tried to do: S = number24 (h), and put the dot in various places, but it still didn't go. If I use just 0.2 and 1.8 the numbers go in the function just fine.
The error I get is:
??? One or more output arguments not assigned during call to 'C:\MATLAB7\work\number24.m (number24)'.
Posts
Let's call n the max number and N the vector.
Define s=0 to hold the sum.
Set up a loop to run from i=1 to n+1. Matlab stupidly starts indexing at 1.
Inside the loop do s=s+((-1)^N(i))*(1/(2*N(i)+1));
Which annoys me because the book doesn't tell you AT ALL on how to do something like this, and Google searching this situation is an exercise in frustration as well.
There are three places to put the period: before the exponential, before the multiplication in the center, and before the division.
1. The first one tells it to raise -1 to each element of n, generating a new vector.
2. The division one is similar, telling it to divide 1 by each element of the 2n+1 vector. The 2n+1 gets handled without special treatment.
4. Since the first quantity and second quantity are both vectors, we need to use the .* to multiply corresponding elements.
For simplicity, we can run the sum() on the resulting vector in the same line.
Also, fuck MATLAB and all involved in its creation.
Also I agree. Matlab is terrible and so is this class for making me use it.
Thank you so, so, so much. I've been wracking my brain on this problem for hours.
A^2 does the linear algebra operation of A*A while A.^2 just squares every element in A.
Also, don't dog on MATLAB. It has some great built-in graphing tools and its ability to create vectors and matrices is really powerful.
I'm not sure what you are trying to accomplish here. In the function you call a variable "h" into it yet you don't use it to calculate SV.
What is h supposed to be? Also, I don't see any vectors in this script. Tell me what this function is supposed to be doing and maybe I can give you a little bit more advice.
number24(0.2) <- see the parenthesis?
Difference between "command" and "function" syntax.
Edit: The specific action that you're seeing is example 7 under "Common Mistakes"
The 0.2 is being changed into a vector:
In quasi-C
[ '0' '.' '2' ]
which numerically is
[ 48 46 50 ]
The disp(h) recognises the original data type, and converts back to the string "0.2", but h+r adds 0.6 to the numeric vector, which is the result you're seeing.
You can't use the . notation to do scalar operations with functions -- as far as I know it only works on many basic math operators. So you'll need to loop, or else write your function to deal with a vector input and provide a vector of output (essentially just do the loop inside your function).
arrayfun()
Remember, make sure your function is defined in it's own functionname.m file, it's in the work path, and you use @functionname for the function handle in arrayfun()
Boom, if I've got a number23.m file, and inside that is my script I have up above... How do I use arrayfun?
I tried S = arrayfun(number23,h) but obviously that's wrong. You say to use the @functionname... So is it... S=arrayfun(number23,@Vfuel,h)?
The function name you're using is Vfuel. Your file must be Vfuel.m
Then you'd do S = arrayfun(@Vfuel,h);
I honestly have no idea where this number23 and number24 stuff is coming from given the code you've posted.
e: Yeah see every time I try that I get the following error:
>> S = arrayfun(@Vfuel,h)
??? Undefined command/function 'arrayfun'.
Something about the @ symbol does that.
No, it's saying that arrayfun() itself isn't defined. A quick bit of googling turned up:
"ARRAYFUN was introduced in MATLAB 7.1 (R14SP3). If you have an older
version, you will not be able to use this function."
What do you get if you do "help arrayfun"?
The undefined command thing suggests it's not finding arrayfun, but it should be. When I try it with your code it works fine.