You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why doesn't this file work?
1 view (last 30 days)
Show older comments
Aidan O'Reilly
on 21 Mar 2011
function [lengths, angles, area] = vectors(A,B,C)
lengths=[norm(C-B), norm(A-C), norm(A-B)];
angles = [acosd(dot(A-B,A-C)/norm(A-B)*norm(A-C)) acosd( dot(B-A,B-C)/norm(B-A)*norm(B-C)),acosd(dot(C-A,C-B)/norm(C-A)*norm(C-B))];
end
This gives me the right answers for lengths when I put in vectors([1,2,-1],[3,4,0],[9,-2,7]), but it's not giving anything for angles, not even an error message.
1 Comment
Andrew Newell
on 21 Mar 2011
Please format the code so it is readable (one line per command, indent two spaces).
Accepted Answer
Matt Fig
on 21 Mar 2011
How are you calling the function? You need to call a multiple return argument function with multiple return args:
[LEN, ANG] = vectors([1,2,-1],[3,4,0],[9,-2,7])
And please, format the code in your question!
.
.
.
EDIT
By calling your function I mean, from the command line (the MATLAB window as you call it), put what I put above, then hit return. You will get two variables out when you do this. See my comment below to re-iterate.
21 Comments
Aidan O'Reilly
on 21 Mar 2011
I don't understand any of what you just said. I don't like Matlab, I'm being forced to use it in my degree. I created the function and then entered "vectors([1,2,-1],[3,4,0],[9,-2,7])" in the Matlab window. Format?
Walter Roberson
on 21 Mar 2011
So, if given your choice, what language would you prefer to implement your scientific programs in?
Matt Fig
on 21 Mar 2011
Instead of
vectors([1,2,-1],[3,4,0],[9,-2,7])
enter:
[LEN, ANG] = vectors([1,2,-1],[3,4,0],[9,-2,7])
in the command window, then hit return. This will create two variables in the command window workspace. The first variable, LEN, is the lengths from your function. The second variable, ANG, is the angles from your function. In your function you don't create the area variable, so you will not be able to create it from the command line. When you do fix your function to create the area variable, then you can use this at the command line:
[LEN, ANG, Ar] = vectors([1,2,-1],[3,4,0],[9,-2,7])
Aidan O'Reilly
on 22 Mar 2011
Okay, it gives an answer now, thanks. It's not giving the right results for the angles though, it gives:
>> [length,angles] = vectors([1,2,-1],[3,4,0],[9,-2,7])
length =
11 12 3
angles =
1.0e+002 *
0 + 2.7800i 1.8000 - 2.2563i 0 + 3.1273i
Aidan O'Reilly
on 22 Mar 2011
Thanks, it's giving the answers like I want now, but not the right numbers. The file is:
function [lengths, angles, area] = vectors(A,B,C)
lengths=[norm(C-B), norm(A-C), norm(A-B)];
angles = [acosd(dot(A,B)/(norm(A,2)*norm(B,2))), acosd(dot(B,C)/(norm(B,2)*norm(C,2))),acosd(dot(A,C)/(norm(A,2)*norm(C,2)))];
end
And entering the vectors
>> [LEN, ANG] = vectors([1,2,-1],[3,4,0],[9,-2,7])
LEN =
11 12 3
ANG =
26.0845 70.8362 94.0447
But the angles are meant to be, 64 102 and 14
Matt Fig
on 22 Mar 2011
Why do you think those are the correct angles? The returned results certainly look plausible to me.
line([0 1],[0 2 ],[0 -1],'color','r'),hold on
line([0 3],[0 4],[0 0],'color','b')
line([0 9],[0 -2 ],[0 -7],'color','k')
xlabel('X')
ylabel('Y')
zlabel('Z')
axis square
rotate3d
Aidan O'Reilly
on 22 Mar 2011
Because this was what I was given.
3. Write a function m-file with three inputs, A, B and C say, each of which is a
3-vector. (The file should be written so that the user can input these as row
vectors or as column vectors.) There should be three outputs: a row vector
containing the lengths a, b and c of the triangle ABC (using the convention
that a refers to the side opposite to the angle at A); a row vector containing the
angles A, B and C in degrees; and a scalar giving the area of the triangle. Run
your m-file with the inputs A(1, 2, -1), B(3, 4, 0) and C(9, -2, 7). The
lengths should then be a = 11, b = 12 and c = 3; the angles should be
A = 64, B = 102 and C = 14, all given to the nearest degree; and the area
should be 16.12 to 2 d.p. (20 marks)
Matt Fig
on 22 Mar 2011
Aidan, you have defined your vectors incorrectly for a triangle. You need to think the problem through - you are not finding the angle between the vectors passed in, but the angles between the vectors which lie along the sides of the triangle!
Av = A-B;
Bv = A-C;
Cv = B-C;
angles = [acosd(dot(Av,Bv)/(norm(Av,2)*norm(Bv,2))),...
acosd(dot(Bv,Cv)/(norm(Bv,2)*norm(Cv,2))),...
acosd(dot(Av,Cv)/(norm(Av,2)*norm(Cv,2)))];
Matt Fig
on 22 Mar 2011
Now you will have to find the area by yourself. Since I now know that this is homework, I won't give you the answer to the mechanics of the problem anymore than I already have. Think about what you are doing, and if you get stuck on the MATLAB coding, you will get help.
Aidan O'Reilly
on 23 Mar 2011
Okay, thank you so much for all your help. It's very refreshing to find people willing to help others selflessly. And just to let you know, to get the above to work
acosd(dot(Av,Cv)/(norm(Av,2)*norm(Cv,2)))]; has to be -Av so it's
acosd(dot(-Av,Cv)/(norm(Av,2)*norm(Cv,2)))]; Or that's what I had to change to get the right answer.
May I ask why you're oppossed to helping with homework? Perhaps it seems I was trying to get the answer written for me? I would like to be able to use Matlab capably but I'm not used to any form of programming. I'm doing a maths degree and we HAVE to use Matlab, even though I'd much rather do all the maths by hand. Had I done a Maths degree 20 years ago I wouldn't have had to use Matlab. But again, thank you so much for your help, I appreciate it so much.
Matt Fig
on 23 Mar 2011
I didn't mean to imply that you were seeking a handout, Aidan. I do help with homework, but only in a general sense. When a homework question comes up, I like to see that the asker has a question about MATLAB usage, has tried to solve the problem after thinking it through, and is not asking for others to solve the problem for him. Again, you were certainly not in the camp of those who put out no effort, so my statement was meant to encourage you to really think through the area question before coming back and saying, "Now show me how to code for the area." Thanks.
Aidan O'Reilly
on 23 Mar 2011
Okay, that's good to know =) . I've tried to write the code for the area. My function now looks like this:
function [lengths, angles, area] = vectors(A,B,C)
Av = A-B;
Bv = A-C;
Cv = B-C;
lengths=[norm(Cv), norm(Bv), norm(Av)];
angles = [acosd(dot(Av,Bv)/(norm(Av,2)*norm(Bv,2))),
acosd(dot(Bv,Cv)/(norm(Bv,2)*norm(Cv,2))),
acosd(dot(-Av,Cv)/(norm(Av,2)*norm(Cv,2)))];
area=(Av).*(Bv)*sind(acosd(dot(-Av,Cv)/(norm(Av,2)*norm(Cv,2))));
end
But my area seems to be giving me an answer for each vector or something. When I enter the command
>> [LEN, ANG, ARE] = vectors([1,2,-1],[3,4,0],[9,-2,7])
I get
LEN =
11 12 3
ANG =
63.6122
14.1411
102.2467
ARE =
15.6359 -7.8179 7.8179
Walter Roberson
on 23 Mar 2011
Aidan, might I suggest that you give some thought to what kind of job you are likely to be able to get with your math degree? As in what kind of mathematics are you planning to specialize in that realistically has obtainable jobs, but where the jobs do not involve substantial computerized processing of the mathematics?
My degree is in mathematics, and a lot of my work is with mathematics in science. The entire sub-department I work for is heavily dependent on mathematics, as are many other sub-departments of the place I work. The department employs about 5000 people. I would be surprised if the department would be willing to hire more than 1 person who did all their math by hand -- the person would have to be a truly brilliant theorist.
There _are_ university professorships available where working by hand might be appropriate, but unless you know some area of mathematics so well that you are *certain* that you can get a professorship, then chances are your future will involve computerized mathematics of some form, and so instead of spending your time resenting "having to use" Matlab, you should spend your time being pleased for having the opportunity to get experience in one of the industry-standard tools that will look good on your resume.
Aidan O'Reilly
on 23 Mar 2011
You make a very valid point. At the moment I'm just sort of seeing how it goes at University. There is a chance after the second year to work within an industry for a year using maths. Which would probably involve mathematical software. If I don't like that I'd probably look to teaching It's not that I don't want to use Matlab and try to resist it. It's more the limited teaching I've had with it, and how overwhelmed I've felt trying some of the Matlab tasks which I've been set. After your help with this one, I feel less resentment to Matlab.
I have the whole of April off from University for Easter and then exams in May. With the long summer I'll have off, I have thought about buying a Matlab book and using that to try and learn how to use Matlab a lot better. As there will be no pressure and I''ll have a lot of free time. Are there any Matlab books you would suggest?
Matt Fig
on 23 Mar 2011
I suggest "Mastering MATLAB" by Duane Hanselman. It is the book that taught me. I was just like you, double majoring in Mathematics/Physics and forced to use MATLAB for a Linear Algebra class. I hated it, it made no sense and it seemed not worthwhile at all. It wasn't until several years later that I had to use it again and came to love it, partially through reading that book.
BTW, if you consider this question closed, please indicate this by selecting a best answer for it.
Walter Roberson
on 23 Mar 2011
Mathworks' site has over 1200 Matlab books listed, I hear!
There is a Question about learning Matlab in which people recommended a specific book; see
http://www.mathworks.com/matlabcentral/answers/1148-how-to-learn-matlab
Walter Roberson
on 23 Mar 2011
Ah, it's the same book Matt just recommended; the other suggestions in that thread are worth reading.
Aidan O'Reilly
on 23 Mar 2011
Thanks, I'll definitely check that out. Wow, that's really encouraging. Have you used many other types of maths software? This is the only one I've be introduced to, but I don't know how widely Matlab is used within companies.
Walter Roberson
on 23 Mar 2011
There is a fairly wide variety of software packages that overlap with Matlab and related packages. See in part, http://en.wikipedia.org/wiki/Computer_algebra_system
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
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.
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
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)