0% found this document useful (0 votes)
3 views12 pages

Programingscilab

The document provides an overview of plotting in Scilab/Matlab, detailing the steps to create and customize plots, as well as the applications of these tools in aerospace engineering. It also discusses the differences between structures and unions in C programming, highlighting their memory allocation and usage. Additionally, it covers functions and procedures in C, emphasizing their modularity, reusability, and the concept of pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Programingscilab

The document provides an overview of plotting in Scilab/Matlab, detailing the steps to create and customize plots, as well as the applications of these tools in aerospace engineering. It also discusses the differences between structures and unions in C programming, highlighting their memory allocation and usage. Additionally, it covers functions and procedures in C, emphasizing their modularity, reusability, and the concept of pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Performance Optimized for large-scale problens Comparable for mosttasks

Plotting in scilab/Matlab

Creating a plot in Scilab involves several steps. Scilab provides functions for basic and advanccd plottíng.
making it a powerful tool for data visualization. Here's the general procedure:

1. Define the Data

You need to specify thedata points you want to plot. These can be vectors or matrices. For a 2D plot, you'll
need two vectors:

X: the independent variable (horizontal axis).

y: the dependent variable (vertical axis).

x-0:0.1:l0: // Detine x from 0 to 10with increments of 0.1

y sin(x):/Define y as a function of x

2. Create the Plot

Use the plot)function to generate the plot. The syntax is

plot(x, y);

3. Customize the Plot

Scilab allows you to customizethe appearanceofthe plot by setting labels, titles, and styles.

Add Title and Labels

Title: Use the title() function.

X-axis Label: Use the xlabel) function.

Y-axis Label: Use the ylabel() function.

Example

title("Sine Wave'"):

xlabel("'x-axis"):

ylabel('"y-axis"):

Change Line Styleor Color

Use additional arguments in plot() to specifyline style, color, and markers.

Example:

yl= cos(x);

plot(x, y. 'b-', x, yl, 'g--): /Sinc in blue solid, Cosine in greendashed

legend("sin()","cos(x)"); Add legend

47
Applications using programs such as Matlab/Scilab in
Engineering
MATLAB (Matrix Laboratory) is a
powerful numerical computing
aerospace engineering for solving environment widely used in
complex problems, simulation,
and visualization.
Key Features:

1. Matrix-BasedComputations:

Ideal for handling vectors and matrices, commonly used in aerodynamics, structural
analysis, and orbital mechanics.

2. Built-In Toolboxes:

Aerospace Toolbox and Simulink for light dynamics, control systems, and simulation.

3. Visualization:

Advanced 2D and 3D plotting tools for analyzing aerodynamic flows, structural

deformations, and more.

4. Programming and Seripting:

for creating custom scripts and functions.


Easy-to-use programming language

Applications in Aerospace Engineering:

1. Flight Dynamics:

Simulating aircraft behavior under various conditions.

2. Orbital Mechanics:

trajectories and interplanetary missions.


Computing satellite

3. StructuralAnalysis:
in aerospace structures.
Analyzing stresses and deformations

4. Control Systems:
autopilots and other control systems.
o Designing and simulating

5. CFD Post-Processing:
(CFD) simulations.
Visualizing results from computationalfluid dynamics
similar capabilities for
numerical
to MATLAB, offering
Scilab is an open-source alternative
computation,simulation, and
visualization.

Scilab
MATLAB
Aspect
Free and open-source
Cost Commercial(requires a licensc)
be extended
Limited but growing, can
Toolboxes Wide range, especially for aerospace

Highly user-friendly with extensive Slightly less polished


interface
Ease of Use
support
forums
Community Large community and robust Smaller community,but active
and Support documentation

46
Example
uid Flow
EOO Data

float

Only one member can be accessed at a time.

union Data d:

di 10:

ponti"i odn'. di):

d.f 5.5:

print"E %.2hn",d.f): /Overwrites'i

Applications of Unions:

Memory Management: Saves memory when only one member is used at a time.

Data Interpretation: Storing data in multiple formats (e.g., interpreting the same data as int or
float).

Comparison: Structuresvs. Unions

Structure Union
spect
Memory Each member has its own memory members
All share the same memory space.
Allocation space.
Size Sum of the sizes of all members. Size of the largest member.

Used for grouping related data with


Usage Used when only one member is active at a time.
independent values.

45
/ Input data into the
structure

printf"Enter Student ID:"):


scanf"od". &student.id):

printt"Enter Student Name ):


seant" 9hn)"student
name). I/Read string input
with spaces
printt"Enter Student Marks: ").

scant("af', &student.marks):

/Displaydata
printt("---StudentInfomation ---n");

printt"ID: %dn'", student.id):

printt"Name: osn'", student. name);

printi("Marks: %.2fn", student.marks):

return 0:

Applications of Structures:

Modeling Real-World Entities: E.g., Points, Students, Cars.

Grouping Data: To manage multiple related data items.

File Handling: Storing and retrieving structured data.

Unions

A union is similar toa structure but with a key difference: all members share the same memory loca

The size of the union is determined by the largest member.

Defining and Using Unions:

union union name

data typememberl;

data type member2;


se-deined data type that allows grouging ot varnables of dieremt ypes nder asl
A seructare is a

Dening and sing Structares

L Declaratiem

Exanple

stract Point

int y

The dot ogerator ()isused to access members ofthe structure for example,

struct Poimt pl =10,20}:


printf "%d y: hdin.plx, pl.yk

Fullexzmple

sract Stadent Define the structure before main

char namc S0)

Boat markss

strict Student student


I/Declare a variable
ofthe structure type

43
Advantages ofUsing
Functions and Procedures
1.
Modularity: Code is divided
into smaller, manageable
sections.
2. Reusability: Functions can be reused in multiple parts of the
3. Readability: Makes program.
the progran
casier to understand.
4. Debugging: Errors are casier to
identify and fix.

Aspect Function Procedure


Return Value Returns a value using return. Does not return a value (uses void).
Used for calculations or data
Purpose
processing.
Used for executing tasks or actions.
Call Usage Can be part of an expression. Called independently.

Pointers, Structures,and Unions in C Programming

A pointer is a variable that stores the memory address of another variable. Pointers are a powerfulfeature
in C that allows for direct memory access and manipulation.

Key Concepts of Pointers:


1. Declarationsyntax:

data type *pointer name:

example: int *ptr;

A pointer is assigned the address of avariable using the address-of operator (&)

int a = 10;
int *ptr =&a:
The value stored at the memory location can be accessedusing the dereference operator

printf("Value: %dn", "ptr):

Applications of Pointers:

a) Dynamic Memory Allocation: Using malloc, calloc, and free.


b) Function Arguments:Pass variables by reference.
c) Data Structures: Used in arrays, linked lists, and trees.
d) Accessing Arrays:

Example

int arr[5]-1.2. 3,4,5}:

int "ptrF arr;

42
Predefined in libraries, c.g.. printf, scanf, sqrt.

2. User-defined Functions:

Created by the programmer for specific tasks.


W
3 Based on Return Type:

Void Function: Does not return a value

void greet)

printt"Hello, World'n'"'):

Value-returning Function: Returns a result.

int multiply(int a, int b) {

return a * b:

Procedures in C
In C, proceduresare conceptually the same as void functions becausethey perform actions but do not
return a value.

Characteristics of Procedures:

They are written using the void return type.

They are used for tasks like displaying output or modifying global variables.

#include <stdio.h>

void printMessage(); 1/ProcedureDeclaration

int main(0 /Main Function

printMessage(); // Procedure Call

return 0;

I/ Procedure Definition after the main function terminates

void printMessage()

print("This is a procedure! n");

41
FUNCTIONS AND PROCEDURES
In C
programming,functions
reusable code. and
procedures are fundamental
building blocks for writing modular and
A
functionis a block of
result. code designed to perform a specific task.It can take input, process it, and return a
There aretwo
main componentsof a
function:
Function Declaration (Prototype): Tells the compiler about the function name,return type, and
parameters. The syntax for declaration is:
eiurm tvpe function
name(parameter list):
Function definition: Contains the actual code. The syntax for
definition is:
returm type function name(parameter list)

Body ofthefunction
returm value; / Optional, depending on the return type

Example

Write a simplecode using afunction

include <stdio.h

int add(int a, int b): I/FunctionDeclaration

int nain) /MainFunction

int result= add(5, 3); /Calling the function

printf("The sum is: odn'" result):

return 0:

/Function Definition at the end of the main code

intadd(int a, int b)

2. Types of Functions

1. Built-in Functions:

40
for (inti 0:irows; i)!
int numnber 1:

for (intj 0:j c ijt)1


printf("%d ", number):

number number * (i -)/G + ):

printt("n"):

return 0:
printlCAltitude: %.0f meters,
Pressure: %.2f
Paln", altitude,
pressure);

return 0;

4. NESTED LOOPS
Any of the above loops can be nested within another loop to
handle multi-level iterative processes.
Example

A program that prints a right-angled triangleusing stars would look like

#include <stdio.h

int main()

int rowS:

printf"Enter the number of rows: "):

scanf("%d", &rows);

for (int i=1;i<= rOws; it){

for (intj = 1:j<= jt) i;

printf("* "):

printf("n'"):

return 0;

Example: A program that prints any size of Pascal's triangle

#include <stdio.h>>

int main(0

int rows;

printt"Enter the number of rows for Pascal's Triangle: ")%

scanf("%d",&rows);

38
printf"Do you want to enter another pair numbers to multiply (YNjny

of
scanf(" %e", &choice):

while (choice Y):


return 0:

3. FOR LOOP
Used when the number of iterations is known beforchand.

Syntax:

for (initialization; condition; increment/decrement)

lcode to be executed

Example

Assuming, assumingthat the pressure at an altitude x meters is given by


P=101325 Xe-0.00012x

Write a C-code using a for loop to print out the pressure


at intervals of1000 m

soln

#include <stdio.h>

int main)

float initialPressure = 101325; // Sea-level pressure

float altitude,pressure;

for (altitude =0: altitude < 5000; altitude = 1 000)

pressure= initialPressure *exp(-0.00012* altitude):

37
2. DO...WHILE
The do. ..whilebehaves almost
exactly like the whileloop but guarantees
body since the condition is checked after at least one execution of the loop
the execution.

Syntax:

do

code to be executed

while (condition):

Use a do...while in place of a while only when the body the loop must execute
of at least one time. The
condition is located at the bottom ofthedo...whileloop, so C can't test the condition until the loop
finishes the first time.

Example

Write aprogram that asks the userfor two numbers and then gives the resulting value if the two inputs are
multiplied. It then asks the user if he or shewould like to multiply two more numbers. As long as the user
keeps typing Y, the program keeps asking for numbers to multiply.Only answering N breaks the loop.

* This program multiplys two numbers and display the result for as long as the user wants.

Answering 'N'
breaks the loop. */

#inchude <stdio.h>

main()

float num1, num2, result;

char choice:

do

printf("Enter your first number to multiply: ");

scanf(" %f", &numl):

your second number to multiply: "):


printf("Enter

scanf("%", &num2);:

result =num1 * num2;

printf("%.2f times %.2f equals 96.2fn\n",

numl, num2, result);

36

You might also like