Constant solving with eval() doesnt work

18 次查看(过去 30 天)
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);

采纳的回答

Rik
Rik 2020-11-17
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
  3 个评论
Rik
Rik 2020-11-17
In that case I hope your teacher is going to tell you about why you shouldn't use eval. (I have taken the liberty of adapting some of Stephen Cobeldick's thoughts on this topic) Introspective programing (e.g. eval, assignin and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem. Also, if you are not an expert, you might find interesting and helpful hints on this page (and if you are an expert, you can still learn from it).
You can always make sure to abide by your teacher's requirement:
eval('num = double(substituted);')
And about your latter question: yes, just use the same function I showed you in my answer:
clc
for param={Q1,Q2,M1,M2}
disp(subs(param{1},[a q0],[2 800]))
end
Flo
Flo 2020-11-19
yeah I already found several topics about not using eval. But I have to follow the restrictions of the teacher.
Actually where do I have to paste the eval expression?
Could you post the complete code again? Very new to Matlab and still learning :D

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by