For complex numerical calculation problems, the computer can be used to solve the problem, that is, using the characteristics of fast computer operation speed and high computer precision, the complex calculation can be completed by repeating simple operations.
Scientific computing is a method of using computers to deal with numerical problems. It is not only abstract and rigorous in mathematical theory, but also practical and practical in programming technology.
The relationship between scientific computing and MATLAB language
Numerical problem - > solution algorithm - > program implementation - > result analysis
Main functions of MATLAB language
① Numerical calculation ② symbolic calculation ③ graphic drawing ④ program flow control ⑤ toolbox
Fundamentals of MatLab programming
1, M file editor
1. Method of creating M file
(1) Run the command edit in the MatLab command line window;
(2) Click new script on the MatLab home page
(3) Select the new script icon on the MatLab home page
(4) Right click the blank space of the current folder box, select "new", and click script;
(5) Use the shortcut key CTRL+N;
2. Open an existing M file
(1) Run the command edit filename in the command line window;
(2) Click open using the folder window;
3. Save M file
(1) Use the Save button on the menu bar
(2) Use the shortcut key CTRL+S;
4. Execution of documents
(1) Open the M file in the editor, and then click Run in the menu bar
(2) Enter the file name of the M file in the command line window. Note: the suffix is not included;
2, Control flow
1. Comparison between if statement and switch statement
if statement | switch Statements |
It is complicated, especially if statements used in nesting | Readable and easy to understand |
When comparing strings, use the strcmp function | Strings of different lengths can be directly compared |
Equality and inequality can be detected | Detect equality only |
2. Cyclic structure
(1) for loop structure
Syntax:
for index = values statements end
Case:
s = 10; H = zeros(s) for c = 1:s for r = 1:s H(r,c) = 1/(r+c-1); end end
(2)while loop structure
Syntax:
while expression statements end
Case:
Find the first element greater than 9999 in the Fibonacci sequence
function [i,z] = fibonacci() a(1) = 1; a(2) = 1; i = 2; while a(i) < 10000 a(i+1) = a(i) + a(i-1) i = i+1; end z = a(i) end
3, Other instructions for control flow
1. return instruction
Using the render instruction in a function will force the execution of the function to be terminated and transfer control back to the main function or command line window.
2. pause instruction
Similar to sleep in other programming languages, you can control the pause and recovery of files. Its syntax format:
Pause: pause the execution of the file and wait for the user to press any key to continue.
pause(n): pause for N seconds before proceeding with file execution.
3. continue instruction
In a nested loop, continue passes control to the iteration nested in the next for or while loop.
Use case:
clc;clear; fid = fopen('magic.m', 'r'); count = 0; while ~feof(fid) line = fgetl(fid); if isempty(line) || strncmp(line, '%', 1) || ~ischar(line) continue end count = count +1; end count fclose(fid);
4. break instruction
During the execution of the loop (such as iterative solution), sometimes we can get the desired results without waiting until the end of the last loop. At this time, the subsequent loops are redundant. We use the break statement to directly end the current loop structure.
Use case:
Find the sum of random sequences until the next random number is greater than the upper limit. Then, use the break statement to exit the loop.
clc;clear; limit = 0.8; s = 0; while j tmp = rand; if tmp > limit break end s = s + tmp; end
5, Function
1. General structure of function
Function declaration line: located at the first line of the function file, starting with the MatLab keyword function, defining the function name and the input / output variables of the function.
Line H1: the first comment line starting with "%" immediately after the function declaration line, which usually describes the function of the function.
Help text area: H1 line and continuous comment line of climate, usually including the meaning and call description of function input / output variables.
Preparation and modification records: the notes behind the text area record all the authors, dates and version numbers of the M documents prepared and modified.
Function body.
The function definition name is consistent with the file save name. When the two are inconsistent, Matlab will ignore the function definition name in the first line of the file and take the file save name as the standard.
Example:
function spir_len = spirallength(d, n, lcolor) % CIRCLE plot a cirale of radius as r in the provided color and calculate its area % d: Pitch of spiral % n: Number of turns of spiral % lcolor: Color of drawing line % spir_len: Circumference of spiral % spirallength(d, n): Spiral with preset parameters in blue % spirallength(d, n, lcolor): utilize lcolor Spiral with preset parameters in color % spir_len = spirallength(d, n): Calculate the circumference of the helix and fill the helix with blue % spir_len = spirallength(d, n, lcolor): Calculate the circumference of the helix and use lcolor Color fill helix if nargin > 3 error('Too many input variables!'); elseif nargin == 2 lcolor = 'b'; end j = sqrt(-1); phi = 0: pi/1000 : n*2*pi; amp = 0: d/2000 : n*d; spir = amp .* exp(j*phi); if nargout == 1 spir_len = sum(abs(diff(spir))); fill(real(spir), imag(spir), lcolor) elseif nargout == 0 plot(spir, lcolor) else error('Too many output variables!'); end axis('square')