Clear Filters
Clear Filters

How can I clear an error "The expression to the left of the equals sign is not a valid target for an assignment?"

13 views (last 30 days)
I tried to solve a problem (see the attachments). But I gor an error "The expression to the left of the equals sign is not a valid target for an assignment". How to solve this issue.
function plot_velocity_vs_eta()
% Parameters
A = 0.5;
Gr = 0.7;
Gc = 0.5;
Kp = 3.0;
beta = 0.5;
Pr = 0.3;
Df = 0.2;
Sc = 0.1;
L0 = 0.5;
Sr = 0.3;
M = [1,2,3,4,5];
% Initialize plot
figure;
hold on;
% Iterate over each Prandtl number
for i = 1:length(M_values)
M = M_values(i);
% Define the system of equations
equations = @(eta, y) [
dy(1)=y(2);
dy(2)=y(3);
dy(3)=(0.5*eta*A*y(3)-Gr*y(4)-Gc*y(6)+(M+Kp+A)*y(2))/(1+(1/beta));
dy(4)=y(5);
dy(5)=(Pr*(0.5*A*eta*y(5))-Pr*Df*Sc*(0.5*eta*A*y(7)+2*A*y(6)-L0*y(6)))/(1-(Pr*Df*Sc*Sr));
dy(6)=y(7);
dy(7)=(Sc*(0.5*eta*A*y(7)+2*A*dy(6))-Sr*Pr(0.5*A*eta*y(5)+2*A*y(4)))/(1-(Sr*Df*Pr));
];
% Define the boundary conditions function
bc = @(ya, yb) boundary_conditions(ya, yb);
% Solve the equations
eta_span = [0, 10];
initial_conditions = [0; 1; 0; 0; 1; 0; 1]; % Initial guesses for f, f', theta, phi, f'', phi'
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
[eta, solution] = ode45(equations, eta_span, initial_conditions, options);
% Calculate velocity (f'(eta))
velocity = solution(:, 2);
% Plot velocity against eta
plot(eta, velocity, '-', 'LineWidth', 2, 'DisplayName', sprintf('M = %.2f', M));
end
% Add labels and legend
xlabel('\eta');
ylabel('Velocity (f''(\eta))');
title('Velocity Profile for Different M Numbers');
legend('Location', 'best');
grid on;
hold off;
end
function res = boundary_conditions(ya, yb)
Bi1 = 0.5;
Bi2 = 0.5;
K0 = 0.3;
% Boundary conditions
res = [
ya(2)= 1 + K0*ya(3);
ya(5) = - Bi1*(1-ya(4));
ya(7) = - Bi2*(1-ya(6));
yb(2) = 0;
yb(4) = 0;
yb(6) = 0;
];
end

Answers (2)

Torsten
Torsten on 25 Jul 2024 at 14:50
You have seven differential equations, but you only specify six boundary conditions. That's mathematically incorrect.
plot_velocity_vs_eta()
Error using bvparguments (line 103)
Error in calling BVP4C(ODEFUN,BCFUN,SOLINIT,OPTIONS):
The boundary condition function BCFUN should return a column vector of length 7.

Error in bvp4c (line 119)
bvparguments(solver_name,ode,bc,solinit,options,varargin);

Error in solution>plot_velocity_vs_eta (line 42)
sol = bvp4c(equations, bc, initial_conditions, options);
function plot_velocity_vs_eta()
% Parameters
A = 0.5;
Gr = 0.7;
Gc = 0.5;
Kp = 3.0;
beta = 0.5;
Pr = 0.3;
Df = 0.2;
Sc = 0.1;
L0 = 0.5;
Sr = 0.3;
M_values = [1,2,3,4,5];
% Initialize plot
figure;
hold on;
% Iterate over each Prandtl number
for i = 1:length(M_values)
M = M_values(i);
% Define the system of equations
equations = @(eta,y) ...
[y(2);...
y(3);...
(0.5*eta*A*y(3)-Gr*y(4)-Gc*y(6)+(M+Kp+A)*y(2))/(1+1/beta);...
y(5);...
(Pr*0.5*A*eta*y(5)-Pr*Df*Sc*(0.5*eta*A*y(7)+2*A*y(6)-L0*y(6)))/(1-Pr*Df*Sc*Sr);...
y(7);...
(Sc*(0.5*eta*A*y(7)+2*A*y(7))-Sr*Pr*(0.5*A*eta*y(5)+2*A*y(4)))/(1-Sr*Df*Pr)];
% Define the boundary conditions function
bc = @(ya, yb) boundary_conditions(ya, yb);
% Solve the equations
eta_span = linspace(0,10,100);
initial_conditions = bvpinit(eta_span,[0; 1; 0; 0; 1; 0; 1]); % Initial guesses for f, f', theta, phi, f'', phi'
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
sol = bvp4c(equations, bc, initial_conditions, options);
% Calculate velocity (f'(eta))
velocity = sol.y(2,:);
eta = sol.x;
% Plot velocity against eta
plot(eta, velocity, '-', 'LineWidth', 2, 'DisplayName', sprintf('M = %.2f', M));
end
% Add labels and legend
xlabel('\eta');
ylabel('Velocity (f''(\eta))');
title('Velocity Profile for Different M Numbers');
legend('Location', 'best');
grid on;
hold off;
end
function res = boundary_conditions(ya, yb)
Bi1 = 0.5;
Bi2 = 0.5;
K0 = 0.3;
% Boundary conditions
res = [ya(2)-(1+K0*ya(3));...
ya(5)-(-Bi1*(1-ya(4)));...
ya(7)-(-Bi2*(1-ya(6)));...
yb(2);...
yb(4);...
yb(6)];
end

Steven Lord
Steven Lord on 25 Jul 2024 at 14:55
You cannot use assignment inside the body of an anonymous function. Change this:
% Define the system of equations
equations = @(eta, y) [
dy(1)=y(2);
dy(2)=y(3);
dy(3)=(0.5*eta*A*y(3)-Gr*y(4)-Gc*y(6)+(M+Kp+A)*y(2))/(1+(1/beta));
dy(4)=y(5);
dy(5)=(Pr*(0.5*A*eta*y(5))-Pr*Df*Sc*(0.5*eta*A*y(7)+2*A*y(6)-L0*y(6)))/(1-(Pr*Df*Sc*Sr));
dy(6)=y(7);
dy(7)=(Sc*(0.5*eta*A*y(7)+2*A*dy(6))-Sr*Pr(0.5*A*eta*y(5)+2*A*y(4)))/(1-(Sr*Df*Pr));
];
to:
% Define the system of equations
equations = @(eta, y) [
y(2);
y(3);
(0.5*eta*A*y(3)-Gr*y(4)-Gc*y(6)+(M+Kp+A)*y(2))/(1+(1/beta));
y(5);
(Pr*(0.5*A*eta*y(5))-Pr*Df*Sc*(0.5*eta*A*y(7)+2*A*y(6)-L0*y(6)))/(1-(Pr*Df*Sc*Sr));
y(7);
(Sc*(0.5*eta*A*y(7)+2*A*dy(6))-Sr*Pr(0.5*A*eta*y(5)+2*A*y(4)))/(1-(Sr*Df*Pr));
];
You will also need to change your boundary condition function. Or actually, since you never use it except in the definition of the anonymous function bc and never use that anonymous function, just eliminate bc and your boundary_conditions function entirely.
Or if your use of ode45 is just for practice in learning how to call the differential equation solvers and you really want to solve this boundary value problem rather than an ordinary differential equation, see the bvp4c or bvp5c functions. The examples on those documentation pages or on the "Solving Boundary Value Problems" topic linked from the end of those function pages should provide you with examples you can adapt for your equations.

Tags

Products


Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!