Turn on thread page Beta
 You are Here: Home >< Maths

# Quick question on matlab... watch

1. Right, so I have a function (similar to the false position method) with one big while loop and every iteration generates values a and b, which change with every iteration... What I'd like to do is create a vector a=[a0,a1,a2...] where a0,a1 etc are the values of a generated with each iteration and same for b

so it looks something like this...

function [a,b]=PlzDieMatlab(f,aini,bini,maxit, tolerance)
*initial conditions*
a=aini
b=bini
etc
while
blablabla
blablabla (either a new value for a or a new value for b is produced)
end

% now the important bit...
a= ?
b= ?
end

anyone have any idea how i can create a vector stating all the different values that have been generated for values of a and b with the iterations?

an example of a correct function tested with values would be ...
a = [2 , 2 , 4 , 4 , 6.9]
b = [9 , 8, 8 , 7.1 , 7.1]

where 7 is the root of the function between the specified intervals

quote from the question .. "This means that the return values a and b are vectors with at most maxit elements,
a(1) sbould be aini, and b(1) should be bini. If the iteration stops after
i<maxit iterations are reached then a and b should contain i elements."

any help appreciated, thanks
2. You might try something like this:

a=zeros(maxit,1);

a(1)=aini;

for i=2: 1:maxit

a(i)=value_of_a_for_this_iterati on;

end

Does that help at all?
3. (Original post by kfkle)
You might try something like this:

a=zeros(maxit,1);

a(1)=aini;

for i=2: 1:maxit

a(i)=value_of_a_for_this_iterati on;

end

Does that help at all?
Are you assuming that maxit is the amount of iterations ?
I should'v said this in the OP but maxit is supposed to be the maximum amount of iterations until the script breaks.
But yes it does help, I think I may see a way of doing it using your method
sleeping pill is getting to my head now so i'll try it out tomorrow, thanks for help
4. one last bump
5. what do you want he's answered your question
6. So, have you solved it?
7. (Original post by Lewk)
Right, so I have a function (similar to the false position method) with one big while loop and every iteration generates values a and b, which change with every iteration... What I'd like to do is create a vector a=[a0,a1,a2...] where a0,a1 etc are the values of a generated with each iteration and same for b

so it looks something like this...

function [a,b]=PlzDieMatlab(f,aini,bini,maxit, tolerance)
*initial conditions*
a=aini
b=bini
etc
while
blablabla
blablabla (either a new value for a or a new value for b)
end

% now the important bit...
a= ?
b= ?
end

anyone have any idea how i can create a vector stating all the different that have been generated for values of a and b with the iterations?

an example of a correct function tested with values would be ...
a = [2 , 2 , 4 , 4 , 7]
b = [9 , 8, 8 , 7 , 7]

quote from the question .. "This means that the return values a and b are vectors with at most maxit elements,
a(1) sbould be aini, and b(1) should be bini. If the iteration stops after
i<maxit iterations are reached then a and b should contain i elements."

any help appreciated, thanks
See kfkle's post or I think you can use

a = [a tempa]
b = [b tempb]

where tempa and tempb are the vector results from each iteration. That'll add them as the ith row I think - you can use

a = [a; tempa]
b = [b; tempb]

to add them as columns. Don't forget to initialise before, something like

a = zeros(1,LENGTH OF a)
b = zeros(1,LENGTH OF b)
8. (Original post by spex)
what do you want he's answered your question
(Original post by kfkle)
So, have you solved it?
hey, thanks for the help but it didn't work, it produced the correct end result but it didn't show what the previous values of a and b correctly.

(Original post by Prime Suspect)
See kfkle's post or I think you can use

a = [a tempa]
b = [b tempb]

where tempa and tempb are the vector results from each iteration. That'll add them as the ith row I think - you can use

a = [a; tempa]
b = [b; tempb]

to add them as columns. Don't forget to initialise before, something like

a = zeros(1,LENGTH OF a)
b = zeros(1,LENGTH OF b)
thanks for response, i'll try this out once I have downloaded matlab on my home computer as i'm away from university now for the easter hols
9. (Original post by Lewk)
hey, thanks for the help but it didn't work, it produced the correct end result but it didn't show what the previous values of a and b correctly.

thanks for response, i'll try this out once I have downloaded matlab on my home computer as i'm away from university now for the easter hols
This works - it adds each 'temp a' as a new row in a. The if statement is to handle the first iteration of the while loop, when a has no value. I tried it here

firstrun = 1;
n=1;
while (n < 5)
if firstrun == 1
a = [1,2,3,4,5];
firstrun = 0;
else
tempa = [1,2,3,4,5];
a = [a; tempa];
end
n = n + 1;
end

You should be able to adapt this to your needs?
10. would you be able to show me what you were able to come up with because I am also stuck

Reply
Submit reply
Turn on thread page Beta

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 11, 2014
The home of Results and Clearing

### 1,340

people online now

### 1,567,000

students helped last year
Today on TSR

### Results day not go to plan?

Get advice and answers to your Qs here

### University open days

1. Keele University
General Open Day Undergraduate
Sun, 19 Aug '18
2. University of Melbourne
Open Day Undergraduate
Sun, 19 Aug '18
3. Sheffield Hallam University
City Campus Undergraduate
Tue, 21 Aug '18
Poll
Useful resources

## Make your revision easier

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams

Can you help? Study help unanswered threads

## Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.