-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write MATLAB summary #23
Draft
JoseFilipeFerreira
wants to merge
6
commits into
master
Choose a base branch
from
jff/mnol
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9ce24d
Write first MNOL draft
JoseFilipeFerreira 2c060dd
separate topics in diferent files
JoseFilipeFerreira 164e0f8
fix link in README.md
JoseFilipeFerreira 2154977
add explanation on variable types
JoseFilipeFerreira 7548859
change order of introduction
JoseFilipeFerreira 06f1fd6
improve aesthetics
JoseFilipeFerreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# MNOL - MATLAB | ||
Nesta secção vamos explorar a parte de MATLAB lecionada em MNOL. | ||
|
||
# Índice | ||
|
||
* [Introdução ao MATLAB](introducao.md) | ||
* [Resolver equações e sistemas de equações](equacoes.md) | ||
* [Estimar valores](estimar_valores.md) | ||
* [Valor do Integral](integral.md) | ||
* [Mínimos Quadrados](minimos_quadrados.md) | ||
* [Quasi-Newton](quasi-newton.md) | ||
* [Método de Nelder-Mead](nelder-mead.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Resolver sistemas de equações | ||
* definir a função | ||
* utilizar o comando **fsolve** | ||
```Matlab | ||
op = optimset('tolfun',TolFun_Value,'tolx',TolX_Value); | ||
x1 = Aproximação_inicial; | ||
format long; | ||
[x,fval,exitflag, output] = fsolve('fun2', x1, op) | ||
``` | ||
NOTA: tolx é o critério de paragem | ||
|
||
- - - - | ||
|
||
# Resolver equação | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Esta devia estar primeiro, no? Equacoes e dps sistemas de equacoes. |
||
* definir a função | ||
* utilizar o comando **fsolve** | ||
```Matlab | ||
op = optimset('tolx',TolX_Value); | ||
x1 = Aproximação_inicial; | ||
format long; | ||
[x,fval,exitflag, output] = fsolve('fun2', x1, op) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Estimar valores | ||
## Polinómio Interpolador de Newton | ||
* obter os grau + 1 pontos em torno do ponto x (por proximidade) | ||
* calcular o polinómio interpolador | ||
```Matlab | ||
pol = polyfit(points_X, points_Y, grau); | ||
``` | ||
* calcular um ponto nesse polinómio | ||
```Matlab | ||
format long; | ||
polyval(pol, point_X) | ||
``` | ||
|
||
## Spline | ||
### Spline Cúbica | ||
```Matlab | ||
sp = spline(points_X, points_Y, x) | ||
``` | ||
|
||
### Spline Cúbica Completa | ||
```Matlab | ||
sp = spline(points_X, [declive_inferior points_Y declive_superior], x) | ||
``` | ||
|
||
NOTAS(apenas utilizar se for explicitado que é necessário): | ||
* se tiver função e não tiver declives | ||
* derivar função | ||
* calcular declive para pontos | ||
* se não tiver função nem declives | ||
* calcular declive dos primeiros dois e últimos dois | ||
* remover o segundo ponto e último ponto dos vetores | ||
|
||
### Segmento de Spline | ||
```Matlab | ||
sp = spline(points_X, points_Y) | ||
% OU | ||
sp = spline(points_X, [declive_inferior points_Y declive_superior]) | ||
sp.coefs | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Mínimos Quadrados | ||
## Polinómios Ortogonais | ||
```matlab | ||
x = [1.5 2 3 4] | ||
y = [4.9 3.3 2 1.5] | ||
|
||
[p,s] = polyfit(x,y,1) | ||
|
||
polyval(p,2.5) | ||
|
||
%Residuo do erro | ||
(s.normr)^2 | ||
``` | ||
|
||
## Modelo não polinomial linear | ||
```matlab | ||
x = [1.5 2 3 4] | ||
y = [4.9 3.3 2 1.5] | ||
|
||
foo = @(c,x) c(1)./x + c(2).*x | ||
[c,resnorm,residual,exitflag,out] = lsqcurvefit(foo,[1 1],x,y) | ||
|
||
%Residuo do erro | ||
resnorm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Introdução | ||
|
||
## Variáveis | ||
MATLAB e dinamicamente tipado o que significa que não se indica qual e o tipo da | ||
variável. Desta forma, para declarar uma variável utiliza-se o operador `=`: | ||
```matlab | ||
>> x = 20 | ||
x = | ||
20 | ||
>> x = 'JBB' | ||
x = | ||
JBB | ||
>> x = [10*2, pi] | ||
x = | ||
20.0000 3.1416 | ||
``` | ||
|
||
- - - - | ||
|
||
## Vetores | ||
Para facilmente declarar um vetor pode-se utilizar o método de MATLAB em que se | ||
indica o valor inicial e final do vetor e o incremento entre cada elemento. | ||
```matlab | ||
>> array = 1:2:9 | ||
array = | ||
1 3 5 7 9 | ||
``` | ||
Omitindo o campo relativo ao incremento assume-se que seja 1. | ||
```matlab | ||
>> array = 1:4 | ||
array = | ||
1 2 3 4 | ||
``` | ||
|
||
- - - - | ||
|
||
## Matrizes | ||
Para declarar uma matriz fila a fila, separando os elementos de cada fila com um | ||
`;` ou com `\n` e a lista de elementos deve ser rodeada de `[]` | ||
```matlab | ||
>> A = [ 1:4 ; 5 6 7 8 ; 9:12 ; 14:17] | ||
A = | ||
1 2 3 4 | ||
5 6 7 8 | ||
9 10 11 12 | ||
14 15 16 17 | ||
``` | ||
Para aceder a uma matriz utiliza-se o operador `()`, podendo ser indicado um | ||
intervalo para obter uma slice da matriz | ||
```matlab | ||
>> A(2,3) | ||
ans = | ||
7 | ||
|
||
>> A(2:4, 3:4) | ||
ans = | ||
7 8 | ||
11 12 | ||
16 17 | ||
``` | ||
|
||
- - - - | ||
|
||
## Funções pré-definidas importantes | ||
| funcao | descricão | | ||
| ----------- | -------------------------------------- | | ||
| ones(n) | matriz de 1s tamanho nxn | | ||
| zeros(n) | matriz nula de ordem n | | ||
| eye(n) | matriz identidade de ordem n | | ||
| (A)’ | transposta da matriz A | | ||
| det(A) | determinante da matriz A | | ||
| rank(A) | característica da matriz A | | ||
| inv(A) | inversa da matriz A | | ||
| diag(A) | diagonal da matriz A | | ||
| triu(A) | matriz triangular superior da matriz A | | ||
| tril(A) | matriz triangular inferior da matriz A | | ||
| norm(A,1) | norma 1 da matriz A | | ||
| norm(A,inf) | norma inf da matriz A | | ||
| A \ b | resolução do sistema linear Ax = b | | ||
|
||
### Exemplo | ||
```matlab | ||
>> b=[1; 3; -6] | ||
>> A=[1 -3 4; 2 5 -2; 3 8 10] | ||
>> A\b %sistema linear Ax=b | ||
ans = | ||
2.2703 | ||
-0.6216 | ||
-0.7838 | ||
``` | ||
|
||
- - - - | ||
|
||
## Definir Funções | ||
```Matlab | ||
function [output] = function_name(input) | ||
output = operation_on_input | ||
``` | ||
**NOTA:** definir na *Function Window* | ||
|
||
### Exemplos | ||
* definir uma função "normal" | ||
```Matlab | ||
function [f] = fun1(x) | ||
f = x^2 + x +1 | ||
``` | ||
|
||
* definir um sistema de equações | ||
```Matlab | ||
function [f] = fun2(x) | ||
f(1) = x | ||
f(2) = x^2 | ||
``` | ||
|
||
**NOTA:** é possível definir lambdas com `@(x) operation_on_x` | ||
|
||
|
||
## Alterar precisão | ||
```matlab | ||
format long % 15 casas decimais | ||
format short % 5 casas decimais | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Mínimos Quadrados | ||
## Polinómios Ortogonais | ||
```matlab | ||
x = [1.5 2 3 4] | ||
y = [4.9 3.3 2 1.5] | ||
|
||
[p,s] = polyfit(x,y,1) | ||
|
||
polyval(p,2.5) | ||
|
||
%Residuo do erro | ||
(s.normr)^2 | ||
``` | ||
|
||
## Modelo não polinomial linear | ||
```matlab | ||
x = [1.5 2 3 4] | ||
y = [4.9 3.3 2 1.5] | ||
|
||
foo = @(c,x) c(1)./x + c(2).*x | ||
[c,resnorm,residual,exitflag,out] = lsqcurvefit(foo,[1 1],x,y) | ||
|
||
%Residuo do erro | ||
resnorm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Método de Nelder-Mead | ||
```matlab | ||
foo = @(x) max(abs(x(1)),abs(x(2)-1)) | ||
|
||
op = optimset('display','iter') | ||
|
||
[x,y,ef,out] = fminsearch(foo,[1 1],op) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Quasi-Newton | ||
Este método apenas calcula o mínimo. Assim, para calcular o máximo utilizamos a | ||
seguinte formula: | ||
``` | ||
max = -min(-f(x)) | ||
``` | ||
|
||
## Sem derivadas | ||
```matlab | ||
foo = @(x) 0.25*x(1)^4-0.5*x(1)^2+0.1*x(1)+0.5*x(2)^2 | ||
fminunc(foo) | ||
% OU | ||
[x,y,ef,out] = fminunc(@(x) 0.25*x(1)^4-0.5*x(1)^2+0.1*x(1)+0.5*x(2)^2,[-1 0.5]) | ||
``` | ||
|
||
> Se `ef < 0` não converge | ||
|
||
> Se `ef = 0` atinge nº max iterações -> aumentar maxIter | ||
|
||
> Se `ef > 0` converge | ||
|
||
## Com derivadas | ||
```matlab | ||
function [f,g] = foo(x) | ||
f = 0.25*x(1)^4-0.5*x(1)^2+0.1*x(1)+0.5*x(2)^2; | ||
g(1) = x(1)^3 %derivada em x1 | ||
g(2) = x(2) %derivada em x2 | ||
end | ||
|
||
op = optimset('gradobj','on') | ||
[x,y,ef,out] = fminunc('foo',[-1 0.5],op) | ||
|
||
``` | ||
|
||
### optimset options: | ||
* 'tolfun',10 | ||
* 'tolx',10 | ||
* 'HessUpdate','DFP' | ||
* 'MaxIter',10000 | ||
* 'MaxFunEvals',10000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Devias explicar melhor estes parametros, onde esta a funcao definida?