Reputation: 141
I want to solve this equation in Matlab.
e=2;
while (e<30)
syms v;
solve('(v-e) = -(0.5*0.2*1.276*v^2*0.3)');
e=e+1;
end
When I write for example "solve('(v-10) = -(0.5*0.2*1.276*v^2*0.3)');" it works. But I need variable "e" in this equation. In some cases, this equation has 2 solutions (negative and positive), but I need only positive solutions. What is the correct syntax? Thank you.
Upvotes: 1
Views: 2986
Reputation: 18484
There's no need to use strings unless you're using a really old version of Matlab. This is the modern and preferred way of using solve
:
syms v positive;
e = 2:29;
s = zeros(length(e),1);
for i=1:length(e)
s(i) = double(solve(v-e(i)==-0.5*0.2*1.276*v^2*0.3,v));
end
However, since this is just a polynomial you can use the roots
function:
e = 2:29;
s = zeros(length(e),1);
for i=1:length(e)
r = roots([0.5*0.2*1.276*0.3 1 -e(i)]);
s(i) = r(r>=0);
end
Upvotes: 1
Reputation: 1012
To add e
to your equation you can concact it as a number to your equation: ['equation part one', num2str(e), 'end of your equation']
.
To only have the positive solution, you can add a condition to your equation ( v>=0
).
Here is an example of a complete solution to your problem:
ans = zeros(1,size(2:29,2));
i = 0;
syms v;
for e = 2:29
i = i+1;
ans(i) = solve(['(v-',num2str(e),') = -(0.5*0.2*1.276*v^2*0.3) and v>=0']);
end
Upvotes: 1