MATLAB: Fsolve system of nonlinear equations with variable parameters

equationsfsolvenonlinearsimultaneousvariable

I have 4 equations for 4 unknowns with several constants that I must solve for. The variables are q_bar, theta_1, theta_in, theta_s. The constants are y1, y2, h1_bar, sigma1_bar, h2_bar, K, D, theta_i. I need to solve the system of equations for various values of theta_i (1,2,3…10) I am trying to do this using a for loop. There are two files, solveeqs.m and equations.m. The solveeqs.m file code is directly below.
%%Solution of Nonlinear System
% This example shows how to solve nonlinear equations.
% Equations are given in a separate file equations.m.
% Solve the system of equations starting at the point [1,1,1,1].
clc
fun = @equations;
x0 = [1,1,1,1]; %initial guess
x = fsolve(fun,x0)
The equations.m file is directly below
%This is the function that computes the system of equations
%Below is a key for our variables:
%x(1)=theta_1
%x(2)=theta_in
%x(3)=q_bar
%x(4)=theta_s
function F = equations(x)
%assign values to constants
y1=2;
y2=0;
h1_bar=0.5;
sigma1_bar=0.25;
h2_bar=1.25;
K=0.025;
D=5;
for theta_i=1:10 %theta_i range of values
%Functions to compute
F(1)=x(1)^(1-y1)-x(2)^(1-y1)-x(3)*(1-y1);
F(2)=K*D*(x(2)^(1-y2)-x(4)^(1-y2))-x(3)*(1-y2);
F(3)=h1_bar*(theta_i-x(1))+sigma1_bar*(theta_i^4-x(1)^4);
F(4)=h2_bar*(x(4)-1);
%plot variables vs theta_i
plot(x,theta_i)
end
What I really need to do is to get the program to run for each of the values of theta_i and then plot the variables vs. theta_i. In other words, q_bar vs theta_i, theta_1 vs. theta_i, theta_in vs. theta_i, and theta_s vs. theta_i.
Can anyone help?

Best Answer

function main
theta_i=[1 2 3 4 5 6 7 8 9 10];
x0 = [1 1 1 1];
for k=1:numel(theta_i)
theta_i_actual = theta_i(k);
xsol = fsolve(@(x)equations(x,theta_i_actual),x0);
theta_1(k) = xsol(1);
theta_in(k) = xsol(2);
y_bar(k) = xsol(3);
theta_s(k) = xsol(4);
end
plot(theta_i,theta_1,theta_i,theta_in,theta_i,y_bar,theta_i,theta_s)
function F=equations(x,theta_i)
...
Best wishes
Torsten.
Related Question