System of non linear equations with parameters - matlab

I'm triying to solve a system of non linear equations in matlab. I am trying to use fsolve to solve the same system but with different parameters.
k1=0.01;
k2=0.05;
k3=10;
k4=100;
Ca0=1;
f2=#(C,tau) [Ca0-C(1)-tau*(k1*C(1)-k2*C(2)+k4*C(1).^2);C(2)-tau*(k1*C(1)-k2*C(2)-k3*C(2))];
t = linspace(0,1);
for i = 1:length(t)
options=optimset('display','off');
solution(i) = fsolve(f2,[2 0],options,t(i));
end
Then I need to plot the solution, but I keep getting Subscripted assignment dimension mismatch.

Related

solving nonlinear equations including max function

I want to solve this system of equations using MATLAB:
I solved the second equation and put it in the first one. Then I tried this:
syms v_star
eqn = max((1.8+0.81*v_star), (-1+0.9*v_star)) == v_star;
solx = solve(eqn,v_star)
And I get this error:
Error using symengine
Input arguments must be convertible to floating-point numbers.
How can I solve this system of equations?

Solving systems of nonlinear equations

Help. I am trying to solve this system of nonlinear equations in MATLAB for a homework assignment. I have tried wolfram alpha and this online equation solver, and neither of them work.
I have tried my graphing calculator and it keeps saying non algebraic variable or expression.
These are my two equations in two unknowns:
.75*(1100)= x*10^(6.82485-943.453/(T+239.711))
25*1100=(1-x)*10^(6.88555-1175.817/(T+224.887)
I don't quite understand how to use MATLAB to solve this system. Please help.
You want the function fsolve in Matlab. Define a function myfun that returns [0,0] at the solution, then run fsolve(myfun,x0). x0 is a guess for the solution.
Define myfun:
function F = myfun(x)
F = [<put modified eqt1 here>;
<put modified eqt2 here>;];
Save it. Then solve:
x0 = [1,1];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(#myfun,x0,options) % Call solver

Partial derivative with Matlab Symbolic Toolbox for Lagrangian equations of motion

I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.
So I would do this in Matlab:
syms t x(t) % t: time, x(t) position dependent on time
m = sym('m'); % mass, a constant parameter
T = m/2*diff(x,t)^2; % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);
But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:
Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
Does anyone know the proper way of handling this?
Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.

ODE System, IVP with differential initial condition

I am trying to model a system of three differential equations. This is a droplet model, parametrized vs the arc length, s.
The equations are:
dx/ds=cos(theta)
dz/ds=sin(theta)
(theta)/ds=2*b+c*z-sin(theta)/x
The initial conditions are that x,z, and theta are all 0 at s=0. To avoid the singularity on d(theta)/ds, I also have the condition that, at s=0, d(theta)/ds=b. I have already written this code:
[s,x]=ode23(#(s,x)drpivp(s,x,p),sspan,x0);
%where p contains two parameters and x0 contains initial angle theta, x, z values.
%droplet ODE function:
function drpivp = drpivp(s,x,p);
%x(1)=theta
%x(2)=x
%x(3)=z
%b is curvature at apex
%c is capillarity constant
b=p(1);
c=p(2);
drpivp=[2/p(1)+p(2)*x(3)-sin(x(1))/x(2); cos(x(1)); sin(x(1))];
Which yields a solution that spirals out. Instead of creating one droplet profile, it creates many. Of course, here I have not initialized the equation properly, because I am not certain how to use a different equation for theta at s=0.
So the question is: How do I include the initial condition that d(theta)/ds=b instead of it's usual at s=0? Is this possible using the built-in solvers on matlab?
Thanks.
There are several ways of doing this, the easiest is to simply add an if statement into your equation:
function drpivp = drpivp(s,x,p);
%x(1)=theta
%x(2)=x
%x(3)=z
%b is curvature at apex
%c is capillarity constant
b=p(1);
c=p(2);
if (s == 0)
drpivp=[b; cos(x(1)); sin(x(1))];
else
drpivp=[2/p(1)+p(2)*x(3)-sin(x(1))/x(2); cos(x(1)); sin(x(1))];
end

How to solve complex system of equations using matlab?

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?
I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0