Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab logic help

  1. Mar 9, 2010 #1
    Hi there, I'm having some issues with logic functions in matlab. I don't use them very much so I'm not too familiar. What I'm trying to do is create an 'm' file for a physics project and I would like a certain progression. I'm trying to create some 'if' statements to do this. So first of all I guess is it possible, second, am I going about it correctly, third, if so then what do I need to change.

    I want to set it up so when the user runs the file they can go to do some calculations or go to the homework assignment(I'm not this far yet). I've been playing around with if / elseif and I have it runnable but for example after the first part runs it prompts the user before displaying the homework. The prompt asks for the string 'start' but will accept anything. Is it possible to make it only accept start? If so how, this will basically allow me to finish it how I want with out other issues.

    My goal in the end is to allow it to go to homework on string homework or calculations on string fun. This is the part I haven't really started yet because I've been trying to get the logic operators working correctly. It seems like this should be do-able but I don't know. Below is the copy of commands I've been working with. Copy and paste if needed (unless you're a crazy wiz) and I tested it just now and it works. What do I add / change? Also I know that at the end of the elseif it needs to be connected to the rest of the program but since I don't know how to do the first part I'm waiting on that.

    Thanks so much everyone I know this was a long post. I really want to blow my class out of the water with this though! :)

    mass_1=100000;%mass of space shuttle in kg
    mass_2=(5.98*10^24);%mass of earth in kg
    orbit_alt=210;%miles on average
    % For fun
    disp(' ')
    disp('For fun')
    disp(' ')
    disp('To calculate potential energy enter the following masses in kg')
    potential=-grav_const*(input('Enter what mass to use ')*input('Enter second objects mass '))/earth_radius;%potential energy "U"
    fprintf(1,'The object''s potential energy is %g N/m \n',potential)
    disp(' ')
    disp('To calculate kinetic energy of an object enter the following mass in kg and velocity in m/s')
    kinetic=.5*(input('Enter what mass to use ')*(input('Enter what velocity to use ')^2));
    fprintf(1,'The object''s kinetic energy is %g N/m \n',kinetic)
    disp(' ')
    disp('To begin the homework follow instructions')
    if(input('When ready, type ''start'' to begin ','s'))
    % 1-distance from cener of earth
    fprintf(1,'The distance of the space shuttle from the center of the earth is %d meters \n',prob_1)
    % 2- Using forces, calculate the velocity of the shuttle required to
    % maintain a circular orbit.
    fprintf(1,'The speed required to maintain circular orbit at 210 miles is %f m/s \n',orbital_velocity)
    elseif(input('Please type ''start''','s'))
    Last edited: Mar 9, 2010
  2. jcsd
  3. Mar 9, 2010 #2
    On an unrelated note I'm having a brain fart about something else. I have a file .dat and want to open it at the begining of my m file so I can use its contents as a variable but I can't for the life of me remember what to type.
  4. Mar 10, 2010 #3
    Any help anyone?
  5. Mar 12, 2010 #4


    Staff: Mentor

    Here's a link to the matlab documentation - http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html.

    Take a look at the Getting Started section. In it, there is a section titled Programming, and in this section there is one titled Flow Control - http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-1931.html [Broken]. This is where you can learn about the logical structures you can use in your coding.
    Last edited by a moderator: May 4, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook