I have a symbolic function in Octave (with symbolic package), e.g.:
syms x;
syms y;
f = x.^2 + y.^2 - sqrt(12);
Which function is used to plot this ? Also, is it possible to plot only specific points, like x,y(2,2) ? Ty !
I don't have that package, so I cannot test, but according to the Internet the basic principle should be:
syms x;
syms y;
f = x.^2 + y.^2 - sqrt(12);
x1=-2:.0001:2;
y1=-2:.0001:2;
# plot3(x1, y1, f(x1,y1)); ## apparently errors out
scatter3(x1, y1, subs(f, {x, y}, {x1, y1}));
Related
The following lines on Octave
>> syms x1 x2
>> jacobian([cos(x1-x2)])
ans = (sym) [-sin(x1 - x2) sin(x1 - x2)] (1x2 matrix)
>> jacobian([cos(x2-x1)])
ans = (sym) [-sin(x1 - x2) sin(x1 - x2)] (1x2 matrix)
gives me the same answer. That's wrong!!! How can I work around this issue?
Regards
Hugo
I would like to have this equation solved symbolically:
x_i = x_0 + \prod_{j = 0}^{i-1}(a_{3-j})
I wrote the following script, which works until I call symprod:
try
pkg load symbolic
end
a = sym('a', [1 3]);
syms x0 i
x0*symprod(a(i), i, [1 3])
The error message says:
error: subscript indices must be integers or boolean
However, this works:
a(2)
ans = (sym) a12
What is wrong with my code?
(I also tried with Matlab Symbolic Toolbox; does not work either, but error message is different.)
The problem is already with the expression a(i), such indexing is not possible:
>> a(i)
error: subscript indices must be integers or boolean
In a situation where a are the integer indices, you are probably better off using prod:
>> prod(a)
ans = (sym) a₁₁⋅a₁₂⋅a₁₃
An alternative is to work with functions:
>> syms fa(i)
>> e=x0*symprod(fa(i), i, [1 3])
e = (sym) x₀⋅fa(1)⋅fa(2)⋅fa(3)
I want to solve the following equation with Octave:
syms y(t) k T T1 % k, T and T1 are constants.
eq1 = diff(y,t)-k*y+k*T == 0
cond=y(0)==T1
dsolve(eq1,cond)
but Octave runs it like this:
{
(sym)
k⋅t
y(t) = T + (-T + y(t))⋅ℯ
}
Octave cannot solve this!!. Does anyone know the answer to this problem?
I am implementing the non-vectorized form of the cost function in octave. This is the code from my .m file
function computeCost(X, y, theta)
sigma=0;
theta0 = 0;
m = length(y);
for i = 1:m
sigma = sigma+ theta*X(i)-y(i);
end;
J = ((sigma)^2)/2*m;
end;
My octave code is:
>> X= [1,1; 1,2; 1,3; 1,4;];
>> y= [2;4;6;8];
>> J = computeCost(X, y, 0.5);
where X and y are matrices. However, I am getting this output on my CLI Window:
Error: computeCost(X, y, 0.5) undefined near line 1, column 5
I've checked my code, there is no apparent issue. Is it because Octave does not accept matrices as parameters for its functions?
The answer to your question is clearly YES: The name MATLAB is an abbreviation of Matrix laboratory. Octave and Matlab are specially designed to facilitate working with matrices.
The problem in your code is: Your function definition is incomplete. You have not defined J as return value. The error message you see is a bit missleading because it should state column 10 as place of the error. When you change the first line of your code to
function J = computeCost(X, y, theta)
It will work as expected and output the value 648.
So I'm trying to knock out this last problem, and I'm following my teacher's guide but my graph seems to still be off, the problem is:
Use the FindRoot command in Mathematica to define an inverse function g(y) to y = f(x) = 3x + tan(x) with the restriction ‑pi/2 < x < pi/2. Use x = tan-1(y) as a starting value. Then use the Plot command to make a graph of g(y).
This is how I wrote it out:
g[y_] := x /. FindRoot[3 x + Tan[x] == y, {x, ArcTan[y]}]
Plot[g[y], {y, (-Pi/2), (Pi/2)}]
I'm not sure exactly what the problem is, but it shows the graph as just being a straight line through the origin. I'm not sure if this is how it's supposed to be (which I assume it's not), but any and all help would be much appreciated!
Having your equation,
3 x + Tan[x] == y
You can check the correctness of the plot of g(y) by plotting y(x):
Plot[3 x + Tan[x], {x, -.4, .4}]
As you can easily see, it is a straight line through the origin. g(y) is inverse of y(x) by definition, so you can get a plot of g(y) it just by exchanging the y and x axes:
Plot[3 x + Tan[x], {x, -.4, .4},
PlotRange -> All] /. {x_Real, y_Real} :> {y, x}