Logic Functions in MATLAB: Troubleshooting and Homework

In summary, the user is trying to create an 'm' file for a physics project and they are having issues with logic functions. They are trying to set up if / elseif statements to do this, but they are having trouble getting the logic operators working correctly. The goal is to allow the user to go to homework or calculations on string fun. This is the part they are not yet started on because they are trying to get the logic operators working correctly. They have copied and pasted some commands, tested them, and they work. What they need to add or change is unknown at this time.
  • #1
jgoff14
16
0
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 without 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
grav_const=(6.67*10^-11);
earth_radius=(6.37*10^6);%meters
orbit_alt=210;%miles on average
grav_accel=9.81;%m/s^2
prob_1=earth_radius+(orbit_alt*1609.344);
orbital_velocity=sqrt(prob_1*grav_accel);
% 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'))
end
 
Last edited:
Physics news on Phys.org
  • #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 beginning 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.
 
  • #3
Any help anyone?
 
  • #4
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 . This is where you can learn about the logical structures you can use in your coding.
 
Last edited by a moderator:
  • #5


Hello, it sounds like you are on the right track with using if/elseif statements to create a progression in your physics project. To answer your questions, yes it is possible to make your program only accept a specific input, such as "start". One way to do this is to use the strcmp function in MATLAB, which compares two strings and returns a logical value (true or false) based on whether they are equal.

For example, you could use the following code to prompt the user to enter "start" and only continue if they do so:

if strcmp(input('When ready, type ''start'' to begin ','s'), 'start')
% code to continue with calculations and homework
end

Additionally, to connect the rest of your program after the elseif statement, you can use the "else" keyword to specify what should happen if the user does not enter "start" as the input.

Overall, your code looks good and it seems like you have a good understanding of how to use logic operators in MATLAB. Keep experimenting and don't be afraid to ask for help if you encounter any further issues. Good luck with your project!
 

1. What is a logic function in MATLAB?

A logic function in MATLAB is a function that takes logical values (true or false) as inputs and outputs a logical value. It is commonly used to make decisions or control the flow of a program.

2. How do I troubleshoot a logic function in MATLAB?

To troubleshoot a logic function in MATLAB, you can use the debugger to step through the code and check the values of variables at different points. You can also use the "disp" function to display the value of a variable and check if it is what you expected it to be.

3. What are some common errors when using logic functions in MATLAB?

Some common errors when using logic functions in MATLAB include using the wrong comparison operator (e.g. using "=" instead of "=="), not properly initializing variables, and forgetting to include necessary parentheses in complex logical expressions.

4. How can I improve the efficiency of a logic function in MATLAB?

To improve the efficiency of a logic function in MATLAB, you can use short-circuiting by placing the most frequently occurring logical conditions first in a logical expression. This will prevent unnecessary evaluations of less frequently occurring conditions.

5. Can you provide an example of using a logic function in a MATLAB homework problem?

Yes, for example, a homework problem may ask you to write a logic function that checks if a given number is prime. Your function could take in the number as an input and use a for-loop to check if the number is divisible by any numbers from 2 to the number-1. If it is not divisible by any of these numbers, the function would output a logical value of true, indicating that the number is prime.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top