1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Matlab. :(

Discussion in 'Entertainment and Technology' started by BudderMC, May 30, 2012.

  1. BudderMC

    Full Member

    Joined:
    Jun 8, 2010
    Messages:
    3,148
    Likes Received:
    2
    Location:
    Ontario, Canada
    Does anyone here know how to program MATLAB? More specifically, is anyone good at plotting in MATLAB?

    From other languages I've done I can do algorithms and stuff no problem, but the way I'm plotting my results seems incorrect. Or rather, it's messing up my legends and stuff. It's hard to explain.

    But yeah... anyone? :stuck_out_tongue_closed_eyes:
     
  2. Swim2Fly

    Regular Member

    Joined:
    May 29, 2012
    Messages:
    46
    Likes Received:
    0
    Location:
    Kansas City
    Gender:
    Male
    Sexual Orientation:
    Gay
    Out Status:
    A few people
    Yeah, I've done quite a bit of plotting on MATLAB over the years, maybe I can be of help! What exactly are you trying to plot?

    Unfortunately, algorithms don't really work too hot when you're plotting stuff, so get ready to hard code!

    Here's an example of what you have to do:

    plot(x1, y, x2, y, etc...); %you can add in colors and textures after the y value, I think.
    legend(label1, label2, etc...);
    title('title'); %might be double quotation marks, been working in c and c++ recently...
    xlabel('x axis'); ylabel('y axis');

    Of course, the semicolons don't do anything unless you're doing a vector operation, but it's good protocol anyway.

    Hope that gets you back on your feet. If you need some clarification or more help, just shout out.
     
  3. BudderMC

    Full Member

    Joined:
    Jun 8, 2010
    Messages:
    3,148
    Likes Received:
    2
    Location:
    Ontario, Canada
    Aww. That's probably my problem right there.

    Anyway, what we were doing (this is an old assignment) was approximation of a differential equation by Euler's Method (if that means anything to you). Basically, the way I set up the step-based approximation was through for/while loops (depending on the question), and stuck the plot command inside the loop. There were three different approximations, each with their own loop, all to be put on the same figure. So I tried to put a legend on after they were done (and fiddled with different spots, but to no avail), but the legend showed the same line three times, but with the proper labels. Always the last loop line too I think.

    That was a load of probably confusing explanation, so I'll post up the code later if that helps any. But offhand, do you know of some fix that I'm missing, or is the way I solved the question just inherently wrong (for plotting)?
     
  4. Swim2Fly

    Regular Member

    Joined:
    May 29, 2012
    Messages:
    46
    Likes Received:
    0
    Location:
    Kansas City
    Gender:
    Male
    Sexual Orientation:
    Gay
    Out Status:
    A few people
    I actually think I'm following, though I could be way off...there are lots of perfectly valid ways to code things...MATLAB is just kind of quirky and likes ugly code better than pretty code. But who am I to judge programming codes? We're supposed to be supportive of everyone's interests on this forum!

    First thing I'd try is adding this command at the end of your loop if you don't already have it:

    hold on;

    This will keep previously graphed lines on the plot. I think that legend labels might overlap, so you may just run into other bugs.

    The approach I'm more familiar with is the following: make separate vectors for each of your different methods (so have an y1, y2, y3, and an x...or multiple x's if each vector has a different index) and then plot them all once you're through the calculations. You may have to basically copy and paste your loop in order to do this properly...not the most efficient or elegant coding, but I bet your computer can handle the extra load. Might take a half-second longer, though.

    If none of that works, definitely feel free to post the code. Always nice to work with real code...or real pseudo-code, in this case.
     
  5. Transse

    Regular Member

    Joined:
    May 29, 2012
    Messages:
    5
    Likes Received:
    0
    Gender:
    Male
    Hi.

    I can recommend for you a few tutors online.
    I found a few useful sites where I've seen adverts for MATLAB students.
    Or you need for a free solution? In this case you can find a lot of helpful tutorials for free .
    Let me know what you choose then I can advice you.
     
  6. BudderMC

    Full Member

    Joined:
    Jun 8, 2010
    Messages:
    3,148
    Likes Received:
    2
    Location:
    Ontario, Canada
    Here's the stuff I was talking about (for the plotting). It seems that there's only 2 loops; my bad. But the idea was to plot the two approximations (the loops) and the solution (the bit after the loops) all on the same plot:

    % initial conditions:
    tQ1 = 0;
    yQ1 = 0;
    stepQ1 = 0.1;

    for i = 0:stepQ1:2.9
    fQ1 = tQ1 - yQ1;
    yQ1_nplus1 = yQ1 + (stepQ1 * fQ1);

    plot([tQ1,tQ1 + stepQ1],[yQ1,yQ1_nplus1],'-..m'); hold on

    tQ1 = tQ1 + stepQ1 ;
    yQ1 = yQ1_nplus1;
    end

    Answer1 = yQ1;

    % initial conditions:
    tQ2 = 0;
    yQ2 = 0;
    stepQ2 = 0.01;

    for i = 0:stepQ2:2.99
    fQ2 = tQ2 - yQ2;
    yQ2_nplus1 = yQ2 + (stepQ2 * fQ2);

    plot([tQ2,tQ2 + stepQ2],[yQ2,yQ2_nplus1],'c'); hold on

    tQ2 = tQ2 + stepQ2;
    yQ2 = yQ2_nplus1;
    end

    Answer2 = yQ2;

    x = (0:0.00001:3);
    y = x - 1 + exp(-x);
    plot(x,y,'-.k');
    I was more just curious than anything; the assignment's already done and handed in weeks ago, so it doesn't matter too much :stuck_out_tongue_closed_eyes:

    As an aside though, does anyone know how to solve differentials in MATLAB using the solver (ode23, ode45, etc.) functions? I know that if it's above first order, you need to rewrite it in terms of other variables, that's the part I'm stuck on...

    Thanks guys, as always!