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 Comments

Copy paste the code here.......image snippet will not help us to help you.
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
Fist you have to define the variables theta, c. You have not defined them. Give thos values first.
thankyou, it still says there is a problem with db though
This line
maxdb = max(db)
Should be:
[maxdb,index] = max(db)
But the code looks illogical.......what exactly you are trying the loop?
"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.
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
What is theta?
I guess:
sint = sin(theta.');
db = -a .* sint - (a.^2 * cos(theta.') .* sint) ./ ...
sqrt(c.^2 - a.^2 * sint .^ 2);
i dont think theta is assigned to anything, it is just an angle
@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?
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

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!