이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Why doesn't this file work?
조회 수: 5 (최근 30일)
이전 댓글 표시
Aidan O'Reilly
2011년 3월 21일
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
Andrew Newell
2011년 3월 21일
Please format the code so it is readable (one line per command, indent two spaces).
채택된 답변
Matt Fig
2011년 3월 21일
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
Aidan O'Reilly
2011년 3월 21일
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
2011년 3월 21일
So, if given your choice, what language would you prefer to implement your scientific programs in?
Matt Fig
2011년 3월 21일
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
2011년 3월 22일
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
Matt Fig
2011년 3월 22일
The angle between two vectors A and B is given by:
acosd(dot(A,B)/(norm(A,2)*norm(B,2)))
Aidan O'Reilly
2011년 3월 22일
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
2011년 3월 22일
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
2011년 3월 22일
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
2011년 3월 22일
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
2011년 3월 22일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
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
2011년 3월 23일
Ah, it's the same book Matt just recommended; the other suggestions in that thread are worth reading.
Aidan O'Reilly
2011년 3월 23일
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
2011년 3월 23일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
