This document discusses functions, nonlinear equations, and ordinary differential equations (ODEs) in MATLAB. It provides examples of defining different types of functions, including anonymous functions with one or two arguments. It also demonstrates using function handles, solving nonlinear equations with fsolve, and solving ODEs with ode45. Key functions and concepts covered include Circle, Eq, DEq, fsolve, ode45, and function handles.
This document discusses functions, nonlinear equations, and ordinary differential equations (ODEs) in MATLAB. It provides examples of defining different types of functions, including anonymous functions with one or two arguments. It also demonstrates using function handles, solving nonlinear equations with fsolve, and solving ODEs with ode45. Key functions and concepts covered include Circle, Eq, DEq, fsolve, ode45, and function handles.
% 2] Modular function (c/c++) % 3] Commented function Plotter(Fcn, x)
% --> Readability and Extensibility y = Fcn(x);
plot(x, y)
%% How to define end
% function [out1, out2, ..] = Fcn1 Plotter(F1, linspace(0,10, 100));
(In1, In2) % .... %% Getting handle of a function % end [s1, p1] = Circle(5) %% % If you a see a repeated lines of F_cr = @Circle; % Getting Handle of a code (at least 2 times), function % you should put it in a function! %% Circle Fcn [s2, p2] = F_cr(5)
function [S, P] = Circle(r) %% Solving System of Nonlinear
Equations S = pi*r.^2; P = 2*pi*r; function y = Eq(x)
end y = zeros(2,1);
y(1) = 4*x(1).^2 + 25*x(2).^2 - 100;
y(2) = 100*x(1).^2 - 196*x(2).^2 - 1225; [S, P] = Circle(10); end % Anonymous Functions % Define simple function in a one line Y0 = fsolve(@Eq, [1; -0.5]) clc, clear Y1 = fsolve(@Eq, [1; 0.5]) F1 = @(x) sin(x).*cos(x); %% Solving Differential Equations Y = F1(10); [T, X] = ode45(@DEq, 0:.1:15, [1;-1]); %% x = linspace(0,10,100); plot(T, X); plot(x, F1(x), 'r--'); legend('x', 'Dx')