# solve function answer is a 2x1 matrix . how to assign it directly to 2 variables ?

13 views (last 30 days)
Ruqaya on 23 Nov 2023
Commented: Ruqaya on 23 Nov 2023
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);
Unrecognized function or variable 'x1'.
##### 2 CommentsShow NoneHide None
Ruqaya on 23 Nov 2023
i need s answer to show the following : x1= "first ans" & x2= ""2nd ans""
Dyuman Joshi on 23 Nov 2023
Note - The use of ezplot is not recommend. Use fplot instead.
Also, look into disp and fprintf / sprintf

John D'Errico on 23 Nov 2023
Edited: John D'Errico on 23 Nov 2023
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)
ans = 1×2
0.4834 0.4685
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))
xm1 = 0.4834
xm2 = 0.4685
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))
y1 =
y2 =
1
Ruqaya on 23 Nov 2023
thanks that was helpful :)

### Categories

Find more on Numeric Solvers in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!