The Student Room Group

Quick question on matlab...

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 :smile:
(edited 13 years ago)
Reply 1
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_iteration;

end

Does that help at all?
Reply 2
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_iteration;

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 :smile:
sleeping pill is getting to my head now so i'll try it out tomorrow, thanks for help
Reply 3
:bump:
Reply 4
one last bump :smile:
Reply 5
what do you want he's answered your question
Reply 6
So, have you solved it?
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 :smile:


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)
Reply 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
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?
(edited 13 years ago)
would you be able to show me what you were able to come up with because I am also stuck

Quick Reply

Latest