This preview shows pages 1–13. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Comparison of C/Ch with MATLAB Outline Variables Functions and Function Files Control Flow Formatted Input and Output File Input File Output Array Manipulation Plotting Computational Arrays Comparison of Operators in MATLAB and Ch MATLAB versus Ch MATLAB Ch Proprietary More toolboxes Typeless software package A superset of C with an international standard Objectbased Ch has all salient features of MATLAB Three times faster than MATLAB A large C/C++ code and user base Easy to integrate with binary C/C++ code Embeddable Better support for Web programming and distance learning Typed language Comments MATLAB C % comments // comments /* comments */ Initialization of Variables MATLAB C x = 3; y = 4.5; a = [1 2 3]; b = [1 2 3; 4 5 6]; double x = 3; double y = 4.5; double a[3] = {1, 2, 3}; double b[2][3] = {1, 2, 3, 4, 5, 6}; Note: MATLAB is typeless. All variables in MATLAB are array of double type. No declaration of variable is required. Programs MATLAB programs must have file extension .m Sample Problem: The system in Figure1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is . The freebody diagram for the system is shown in Figure1 (b). Figure1: The system diagram and FBD of a sample problem The nomenclature related to the modeling of the system.is listed below. m mass of the body x position of the body v velocity of the body a acceleration of the body g gravitational acceleration  friction coefficient f friction force N normal force Equation of motion: The equation of the motion of the system can be derived based on the Newton's second law. N = mg (1) f = N (2) pf = ma (3) From equation (1), (2) and (3), the formula for calculating the acceleration of the rigid body can be derived as follows. a = (p mg)/m (4) Problem Statement: For the system shown in Figure1(a), given m = 5 kg, g = 9.81 m/s 2 , = 0.2. The external force p = 20 N. Write a program to calculate the acceleration a. Program: accelvar.c #include <stdio.h> int main() { double a, mu, m, p; mu = 0.2; m = 5; p = 20; a = (pmu*m*9.81)/m; printf("Acceleration a = %f\n", a); return 0; } Output: Acceleration a = 2.038000 Program: accelvar.ch double a, mu, m, p; mu = 0.2; m = 5; p = 20; a = (pmu*m*9.81)/m; printf("Acceleration a = %f\n", a); Output: Acceleration a = 2.038000 Program: accelvar.m mu = 0.2; m = 5; g = 9.81; p = 20; a = (pmu*m*g)/m; fprintf('Acceleration a = %f\n\n', a) Output: Acceleration a = 2.038000 Functions and Function Files Functions in MATLAB must be defined in separate function files....
View
Full
Document
This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.
 Fall '07
 Cheng

Click to edit the document details