0% found this document useful (0 votes)
72 views

Matlab Fsolve

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.

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Matlab Fsolve

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.

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Session 6: Functions, Nonlinear Equations, ODEs

%% M-File: function F3 = @(x) F1(x) + F2(x, 0);

% Programming Priciples: ezplot(F3)

% 1] Simplicity %% Function Handle = Pointer to


% 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')

%% Function Handle (2 args)

F2 = @(x, y) sin(x) + cos(y);

Y = F2(10, 20);

ezmesh(F2)

%% Function Handle (1 arg)


MATLAB Course By: Mansour Torabi

You might also like