MatlabIntro

pdf

School

University of Florida *

*We aren’t endorsed by this school

Course

2217

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

6

Uploaded by erikanoel34

Report
Exercise 1: Run the Script File § Now run your script file by clicking on the Green Arrow in the m-file editor window. § Notice that every single variable defined in the script file (radius, area, and circum) appears in the Workspace Window. Area and circum are also displayed in the Command Window because of the disp command. § Clear the workspace window by typing clear at the command prompt. § At the command prompt, type the name of your script file: >> CircleScript. Note, that the results are exactly the same as before. % Radius is inputted and the area and circumference are calculated radius = 4; area = pi*radius.^2; disp( 'area =' ); disp(area); area = 50.2655 circum = 2*pi*radius; disp( 'circumference =' ); disp(circum); circumference = 25.1327 Exercise 2: Another Script File § Create a new script file called SphereScript that will compute and display the surface area and the volume of a sphere. % Radius is inputted and the surface area and volume are calculated radius = 4; surf_area = 4*pi*radius.^2; disp( 'surface area =' ); disp(surf_area); surface area = 201.0619 volume = (4/3)*pi*radius.^3; disp( 'volume =' ); disp(volume); volume = 268.0826 Problem 3 Plot tan(x) from 3π/2 to 3π/2. 1
Since tan(x) is undefined (approaches ∞) at odd multiples of π/2, your graph will look seriously distorted because the values are so huge as the angle approaches the asymptotes that the smaller values get swamped out. >> axis([-3*pi/2 3*pi/2 -10 10]) to make a better plot of tan(x) from 3π/2 to 3π/2 . The axis commands specifies a range for the x-axis (3π/2 to 3π/2) and a range for the y-axis (-10 to 10). Using the axis command after the plot statement will re-set the axis. x = linspace(3*pi/2, 3*pi/2, 1000); % Generate x values from 3*pi/2 to 3*pi/2 y = tan(x); % Calculate corresponding y values for tan(x) plot(x, y); % Plot tan(x) axis([-3*pi/2 3*pi/2 -10 10]); % Set axis ranges xlabel( 'x - \pi' ); % Label x-axis ylabel( 'tan(x)' ); % Label y-axis title( 'Plot of tan(x) from 3\pi/2 to 3\pi/2' ); % Title of the plot Loop Homework - Problem 4 2
Write a MATLAB script that will allow a user to play the dice game: Under and Over Seven. The rules are pretty simple: 1. The game is played with two dice 2. The player specifies how much money he/she is betting on the roll 3. The player bets whether he/she will roll Under 7, Over 7, or Exactly 7 4. The player specifies how much money he/she is betting on the roll 5. The player rolls the dice 6. If the player bets incorrectly, he/she loses the amount of money in the bet 7. If the player bets correctly, the payoff is 1:1 for Under 7 and for Over 7 (that is, if the bet was $1 then the player wins $1) and is 4:1 for Exactly 7 (that is, if the bet was $1 then the player wins $4 for betting on and successfully rolling a 7). § Your program should begin by asking the player the total amount of money that he/she has to play this game. Throughout the game, your function will keep track of how much money the player has left based on wins and losses. § The player should then be asked to place his/her bet (Over 7, Under 7, Exactly 7) and the amount of money for the bet. If the player tries to bet more than he/she has, prompt for a new bet amount. § Roll the two dice and display the results (the number on each dice and win or lose). Use • the randi function to do this.§ Calculate and display to the user his/her new balance (total amount of money he/she has • now). § If the balance is greater than zero, ask the user if he/she would like to play again. If the user says answers yes, prompt for a bet and a betting amount and run through the cycle • again. § The game (program) should end when the user has no money left to bet or the user decides to quit. clc,clear % %% Dice Game Under and Over Seven prompt= "How much money are you starting with: " ; player_1=input(prompt); % The game will run 10 times, which is enough time for the user to quit the % game or lose for k=1:10 if player_1<= 0 caution=( "You need to start the game with more than 0 : " ); player_1=input(caution); %Here are some parameters such that the user chooses a starting amount %greater than zero. else player_1=player_1; end placed_bet= "Place your bet on Under 7, Over 7, or Exactly 7: " ; 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
% In order for the bet to work, the user must imput either of the following commands % Under 7, Over 7, or Exactly 7. If any of the following variations like under 7, over, % exactly, etc. are used as inputs, the game won't work properly. place_bet=input(placed_bet, "s" ); money_prompt= "How much money are you betting: " ; bet=input(money_prompt); if bet>player_1 warning1=( "Your bet can't exceed your current amount of " + player_1 + " , choose a new amount: " ); bet=input(warning1); if player_1 < bet >=0 warning3=( "Your bet needs to be higher than zero or lower than your current balance, place a new bet: " ); bet=input(warning3); end elseif bet <= 0 warning2=( "Your bet needs to be higher than zero, choose a new amount: " ); bet=input(warning2); if player_1 < bet >=0 warning3=( "Your bet needs to be higher than zero or lower than your current balance, place a new bet: " ); bet=input(warning3); else bet=bet; end end if bet <=0 | player_1<bet disp( "Game Over" ) break end % This are the parameter that prohibit bets that are larger than the current % amount of money which the user has and those lower or equal to zero. player_1=player_1-bet; dice_1=randi(7); disp( "First roll " + dice_1) dice_2=randi(7); disp( "Second roll " + dice_2) sum=dice_1+dice_2; disp( "Result is : " + sum) 4
% Here the dices are rolled and the overall sum of the dices is calculated if sum< 7 outcome= "Under 7" ; disp(outcome) elseif sum > 7 outcome= "Over 7" ; disp(outcome) elseif sum == 7 outcome= "Exactly 7" ; disp(outcome) end %This section of the code relates the options of the bet to the %sum of the dices. if place_bet == outcome && sum==7 player_1=player_1+(4*bet); disp( "Current balance is " + player_1) end if place_bet ~= outcome player_1=player_1; disp( "Current balance is " + player_1) elseif place_bet == outcome && sum < 7 player_1=player_1+bet; disp( "Current balance is " + player_1) elseif place_bet == outcome && sum > 7 player_1=player_1+bet; disp( "Current balance is " + player_1) end % Here the system checks if the conditions of the bet, that the user % chose, satisfies the outcome of the dices. if player_1 <= 0 disp( "Game Over" ) break end player_prompt= "Continue playing the game or quit: " ; result_prompt=input(player_prompt, "s" ); if result_prompt== "quit" disp( "Game Over" ) break end % This section covers the parameters for the game to end, the user is asked to % quit the game and if so the game stops or the game will see if the % user can keep playing based on the money the user has left. 5
end disp( "Thanks for playing" ) 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help