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 on coursehero.com

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