Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    14
    ReputationRep:
    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
    Offline

    0
    ReputationRep:
    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?
    • Thread Starter
    Offline

    14
    ReputationRep:
    (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
    • Thread Starter
    Offline

    14
    ReputationRep:
    :bump:
    • Thread Starter
    Offline

    14
    ReputationRep:
    one last bump
    Offline

    4
    ReputationRep:
    what do you want he's answered your question
    Offline

    0
    ReputationRep:
    So, have you solved it?
    Offline

    0
    ReputationRep:
    (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)
    • Thread Starter
    Offline

    14
    ReputationRep:
    (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
    Offline

    0
    ReputationRep:
    (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?
    Offline

    0
    ReputationRep:
    would you be able to show me what you were able to come up with because I am also stuck
 
 
 
Poll
Who is your favourite TV detective?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

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.