HOW DO I DO THAT IN MATLAB SERIES?
In this series, I am answering questions that students have asked me about MATLAB. Most of the questions relate to a mathematical procedure.
How do I solve a nonlinear equation if I need to set it up?
% Language : Matlab 2008a; % Authors : Autar Kaw; % Mfile available at % http://numericalmethods.eng.usf.edu/blog/integration.m; % Last Revised : March 28, 2009; % Abstract: This program shows you how to solve a nonlinear equation % that needs to set up as opposed that is just given to you. clc clear all
disp('ABSTRACT') disp(' This program shows you how to solve') disp(' a nonlinear equation that needs to be setup') disp(' ') disp('AUTHOR') disp(' Autar K Kaw of http://autarkaw.wordpress.com') disp(' ') disp('MFILE SOURCE') disp(' http://numericalmethods.eng.usf.edu/blog/nonlinearequation.m') disp(' ') disp('LAST REVISED') disp(' April 17, 2009') disp(' ')
ABSTRACT This program shows you how to solve a nonlinear equation that needs to be setup AUTHOR Autar K Kaw of http://autarkaw.wordpress.com MFILE SOURCE http://numericalmethods.eng.usf.edu/blog/nonlinearequation.m LAST REVISED April 17, 2009
Solve the nonlinear equation where you need to set up the equation For example to find the depth 'x' to which a ball is floating in water is based on the following cubic equation 4*R^3*S=3*x^2*(R-x/3) R= radius of ball S= specific gravity of ball So how do we set this up if S and R are input values
S = 0.6000 R = 0.0550
disp('INPUTS') func=[' The equation to be solved is 4*R^3*S=3*x^2*(R-x/3)']; disp(func) disp(' ')
INPUTS The equation to be solved is 4*R^3*S=3*x^2*(R-x/3)
Define x as a symbol
syms x % Setting up the equation C1=4*R^3*S C2=3 f=[num2str(C1) '-3*x^2*(' num2str(R) '-x/3)'] % Finding the solution of the nonlinear equation soln=solve(f,x); solnvalue=double(soln);
C1 = 3.9930e-004 C2 = 3 f = 0.0003993-3*x^2*(0.055-x/3)
disp('OUTPUTS') for i=1:1:length(solnvalue) fprintf('\nThe solution# %g is %g',i,solnvalue(i)) end disp(' ')
OUTPUTS The solution# 1 is 0.0623776 The solution# 2 is 0.14636 The solution# 3 is -0.0437371