solve function answer is a 2x1 matrix . how to assign it directly to 2 variables ?
7 次查看(过去 30 天)
显示 更早的评论
v=[2;0;2;1;0;9;3;9;6];
m=max(v);
n=mean(v);
syms x
f=@(x) 2*n-x;
g=@(x) (n/6)*x.^2-2*m;
ezplot(f,[-10,10]);
hold on
ezplot(g,[-10,10]);
grid on
title ('graphs of f(𝑥) and g(𝑥)')
legend ('f(x)','g(x)');
s=round(solve( 2*n-x == (n/6)*x.^2-2*m ,x),5);
'x1=manually input (1st ans given by solve function above)';
'x2=manually input (2nd ans given by solve function above)';
a=int(2*n-x-(n/6)*x.^2+2*m,x1,x2);
2 个评论
回答(1 个)
John D'Errico
2023-11-23
编辑:John D'Errico
2023-11-23
For example, I'll compute the mean of an array, which here will generate a vector of length 2.
A = rand(10,2);
mean(A,1)
Define this function handle:
splitvec = @(x) deal(x(1),x(2));
Now I can use that little toy I just built.
[xm1,xm2] = splitvec(mean(A,1))
It directly takes a vector of length 2, and returns 2 distinct variables. Personally, I will tell you that is a bad idea. That it is better to just return a vector, and index into it.
Of course, this works as well on symbolic results.
syms y
[y1,y2] = splitvec(solve(y^2 - 1 == 0,y))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!