MATLAB: Constant solving with eval() doesnt work

constantsevalMATLABsolve

Hi guys,
im rather new to Matlab, especially to the topic.
I need to integrate the q1 and q2 functions two times. Then I get the constants C1, C2, C3, C4. I have to solve these, considering the g1 functions (boundary conditions).
Later on I try to solve the functions with eval() and continue to get the solved constants.
But in the command window I get something like: "M2(x) = C4 + (x*(6*C3 + 6*q0*x))/6 – (q0*x^3)/(6*a)".
Does somebody know how to get the values of the constants for a=2 and q0=800? We have to use the eval() function….
Thanks a lot 🙂
clear
clear all
clc
% Definition symb vars
syms x a b C1 C2 C3 C4 q0
% define symbolic functions
q1(x) = 0*x
q2(x) = -(q0/a)*x + 2*q0
% symb. Integration
Q1(x) = int(q1,x) + C1
Q2(x) = int(q2,x) + C3
M1(x) = int(Q1,x) + C2
M2(x) = int(Q2,x) + C4
% Boundary conditions
g1 = subs(M1,x,0) == 0;
g2 = subs(M2,x,2*a) == 0;
g3 = subs(Q1,x,a) == subs(Q2,x,a);
g4 = subs(M1,x,a) == subs(M2,x,a);
% solve symb. functions
%C = solve(q,C)
[C1, C2, C3, C4] = solve([g1, g2, g3, g4], [C1, C2, C3, C4]);
% insert solutions
%Y(x) = eval(Y)
Q1(x) = eval(Q1);
Q2(x) = eval(Q2);
M1(x) = eval(M1);
M2(x) = eval(M2);
% insert values
a = 2;
q0 = 800;
Q1_num = eval(Q1);
% fprintf ("C1 = %f\n", C1);
%a = 5;
%b = 2;
%Y_num = eval(Y)
%fprintf ("C1 = %f\n", Q1_num);

Best Answer

Who told you you needed eval? If you want to substitute values into your symbolic function, you can use subs. And you shouldn't use clear all. Don't ignore the warning mlint is giving you. At most you need clc,clear here.
clear,clc
% Definition symb vars
syms x a b C1 C2 C3 C4 q0
% define symbolic functions
q1(x) = 0*x ;
q2(x) = -(q0/a)*x + 2*q0;
% symb. Integration
Q1(x) = int(q1,x) + C1;
Q2(x) = int(q2,x) + C3;
M1(x) = int(Q1,x) + C2;
M2(x) = int(Q2,x) + C4;
% Boundary conditions
g1 = subs(M1,x,0) == 0;
g2 = subs(M2,x,2*a) == 0;
g3 = subs(Q1,x,a) == subs(Q2,x,a);
g4 = subs(M1,x,a) == subs(M2,x,a);
% solve symb. functions
sol = solve([g1, g2, g3, g4], [C1, C2, C3, C4]);
% insert solutions
C = subs(struct2cell(sol).');
substituted = subs(C,[a q0],[2 800]);
num = double(substituted);
% display the result as symoblic and as value
fn = fieldnames(sol);fprintf('%s\n',sprintf(' %s ',fn{:}))
C1 C2 C3 C4
disp(substituted)
disp(num)
1.0e+03 * -0.2667 0 -2.6667 2.1333