how do i create an array from a for loop

i need to create 2 new arrays but it says there is a problem with maxdb in line 12

11 件のコメント

KSSV
KSSV 2021 年 5 月 11 日
Copy paste the code here.......image snippet will not help us to help you.
Katherine Carcatsis
Katherine Carcatsis 2021 年 5 月 11 日
編集済み: KSSV 2021 年 5 月 11 日
a = (10:0.1:14);
db = - a.*sin(theta) - (a.^2*cos(theta).*sin(theta))/(c.^2 - a.^2*sin(theta).^2).^(1/2);
plot(theta, db);
maxdb=[];
maxtheta = [];
for a = 10:0.1:14
c = 30-a;
db;
maxdb = max(db)
maxtheta = max(theta)
maxdb = [max(db);db(index)]
maxtheta = [max(theta);theta(index)]
end
KSSV
KSSV 2021 年 5 月 11 日
Fist you have to define the variables theta, c. You have not defined them. Give thos values first.
Katherine Carcatsis
Katherine Carcatsis 2021 年 5 月 11 日
thankyou, it still says there is a problem with db though
KSSV
KSSV 2021 年 5 月 11 日
This line
maxdb = max(db)
Should be:
[maxdb,index] = max(db)
But the code looks illogical.......what exactly you are trying the loop?
Jan
Jan 2021 年 5 月 11 日
"it says there is a problem" - then please post a complete copy of the error message. It can contain important details.
"with maxdb in line 12" - show us, which is the "line 12". If the users count, they have to guess, that you did not crop initial blank lines or comments.
The purpose of the code is not clear:
  • "db;" This line displays the contents of db, while surpressing the output. So nothing happens.
  • The body of the loop does not depened on the loop counter "a".
  • "index" is undefined.
  • "c = 30-a;" c is not used anywhere.
  • maxdb is overwritten twice in each iteration.
  • "theta" is undefined.
Katherine Carcatsis
Katherine Carcatsis 2021 年 5 月 11 日
sorry guys,
so the purpose of the code is:
db is the derivative of a formula, b, and for each value of 'a', from 10-14 with increments of 0.1 are to be substituted into db to find the maximum value db can take and the maximum value theta can take and store this all in a table format.
here is what comes up when i run the code:
>> q12
Arrays have incompatible sizes for this operation.
Error in q12 (line 4)
db = - a.*sin(theta) - (a.^2*cos(theta).*sin(theta))/(c.^2 - a.^2*sin(theta).^2).^(1/2);
Related documentation
Jan
Jan 2021 年 5 月 11 日
編集済み: Jan 2021 年 5 月 11 日
What is theta?
I guess:
sint = sin(theta.');
db = -a .* sint - (a.^2 * cos(theta.') .* sint) ./ ...
sqrt(c.^2 - a.^2 * sint .^ 2);
Katherine Carcatsis
Katherine Carcatsis 2021 年 5 月 11 日
i dont think theta is assigned to anything, it is just an angle
Jan
Jan 2021 年 5 月 11 日
@Katherine Carcatsis: The statement "theta is just an angle" is not meaningful. How should Matlab calculate the value of db, if you use a variable, which is "just an angle"?
The purpose of the loop is still unclear, but now it is getting clear, that you do know, what the first line should do.
Which problem should the code solve? What are the inputs and what do you want to calculate?
Katherine Carcatsis
Katherine Carcatsis 2021 年 5 月 11 日
hi, thankyou for your help. my code now works!! if i can just ask one more question, how do i display all my results in a single table format??
table(a,largestdb,largesttheta)
i have this line of code to display it in a table but each value for a creates a new 1x3 table

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2021 年 5 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by