Matlab wont allow me to work with a large amount of values....and what is the function of the symbol '

2 次查看(过去 30 天)
My question is based on the following:
In the following, R can take any value between 5 and 6. Work out the value of I for R = 5.00, 5.01, 5.02, ..., and 6. Then find the average value of I. I =30/R*sqrt(R2 + pi^2)
Well after many sleepless nights i came up with this:
for R = [5: 0.01: 6]
A = 30
B = R.^2;
end
B
C = 15*pi.^2
for D = B .+ C
end
D
for E = sqrt(D).*R;
end
E
for I = A./E;
end
I
My questions are: 1) i forgot why it is neccesary to but the ' sign next to the matrix of R 2) matlab only computes the first 30 values i cannot get it to do the other 70 i.e it stops at 5.30 in matrix R.
Please help, i'll but you a Ferrari if i ever make it big :D

回答(1 个)

Stephen23
Stephen23 2015-2-12
编辑:Stephen23 2015-2-13
  1. Actually the ' is not required, and in fact it prevents your code from looping over the elements of the vector R. Or perhaps this is the point? In which case the whole for loop is irrelevant anyway.
  2. ... I was going to continue improving this code, but it has so many syntax errors and mistakes that it is hard to know where to start to analyze it. What is
for D = B + C end D for E = sqrt(D).*R; end
supposed to do? Is that one loop or two? What happens inside these loops? Nothing?
Please learn to use the MATLAB editor and pay attention to the syntax hinting and error highlighting! Those messages are not just put there to make the editor more colorful. Use them. MATLAB also has great documentation for every function: learn to use it.
A much better solution is for you to forget about loops, and learn about vectorization in MATLAB. Like this:
R = 5:0.01:6
P = R.^2
Note how MATLAB can perform the .^2 operation simultaneously on all elements of the vector R. Now try it with sqrt:
S = sqrt(P)
It also can operate on all elements simultaneously! No loops required! You can even put them together like this:
R = 5:0.01:6
S = sqrt(R.^2)
I have now shown you how two of the five operators can be vectorized. You can figure out the rest for your homework. Here is a BIG hint: your final code could look very similar to how the maths is written in the question that you were given.
  2 个评论
S Weinberg
S Weinberg 2015-2-13
编辑:S Weinberg 2015-2-13
You came down hard on me but i'm guessing that is the only way to learn Matlab. Let me please take a look at your questions & answers this weekend Stephen, i am very new to Matlab!Looking at your question i see that your solution makes more sense, i was trying to implement the loop because that is the way my study guide informed me to do it.
And thanks i hope to come back to you the weekend regarding maybe a few more questions?
Stephen23
Stephen23 2015-2-13
编辑:Stephen23 2015-2-13
Of course you can ask more questions. There is also plenty of good advice within the MATLAB documentation too: every function has its own explanation (e.g. sqrt ).
Three tips for a beginner:
  1. MATLAB has a great IDE, with code editor, a visible workspace and lots of other neat tools. Get comfortable with it, and pay attention to the editor if it tells you that something is wrong. Use the f1 key to bring up help on any function that you are typing.
  2. MATLAB has great documentation for every function and even articles introducing different topics and algorithms. Ten minutes investigating the documentation contents (on the left-hand side) will be time well spent. Get comfortable with these contents, they can be very useful!
  3. Try these tutorials .
Have fun learning!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by