VSCode not rendering Markdown with latex in Jupyter notebook - visual-studio-code

The following Markdown code from a Jupyter notebook produces an error in VSCode. Some initial googling suggests this has to do with Latex rendering in markdown in vscode? Any suggetions on how to fix it, whether it's a VSCode setting, a VSCode extension, or something else? For what it's worth, the equation renders just fine in a browser (i.e., outside of VSCode).
<a name="3.5"></a>
### 3.5 Gradient for regularized logistic regression
In this section, you will implement the gradient for regularized logistic regression.
The gradient of the regularized cost function has two components. The first, $\frac{\partial J(\mathbf{w},b)}{\partial b}$ is a scalar, the other is a vector with the same shape as the parameters $\mathbf{w}$, where the $j^\mathrm{th}$ element is defined as follows:
$$\frac{\partial J(\mathbf{w},b)}{\partial b} = \frac{1}{m} \sum_{i=0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - y^{(i)}) $$
$$\frac{\partial J(\mathbf{w},b)}{\partial w_j} = \left( \frac{1}{m} \sum_{i=0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - y^{(i)}) x_j^{(i)} \right) + \frac{\lambda}{m} w_j \quad\, \mbox{for $j=0...(n-1)$}$$

I could reproduce your error...\mbox{} seems to be problematic.
Under the assumption, that you are using \mbox for the correct spacing, there is a simple alternative to fix it. \, creates a half space width...
The following code should do the trick:
In this section, you will implement the gradient for regularized logistic regression.
The gradient of the regularized cost function has two components. The first, $\frac{\partial J(\mathbf{w},b)}{\partial b}$ is a scalar, the other is a vector with the same shape as the parameters $\mathbf{w}$, where the $j^\mathrm{th}$ element is defined as follows:
$$\frac{\partial J(\mathbf{w},b)}{\partial b} = \frac{1}{m} \sum_{i=0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - y^{(i)}) $$
$$\frac{\partial J(\mathbf{w},b)}{\partial w_j} = \left( \frac{1}{m} \sum_{i=0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - y^{(i)}) x_j^{(i)} \right) + \frac{\lambda}{m} w_j \quad\, for\,\,j=0...(n-1)$$
for\,\,j=0...(n-1)
With the result...
Hope this fix helps. I am still looking for an explaination though

Related

How can I write with latex character in xlabel in matlab

I would write my xlabel with Latec character so I used this code
x = -10:0.1:10;
y = [sin(x); cos(x)];
plot(x,y)
xlabel('$\mathbb{x}$','Interpreter','latex')
but I have this warning message
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
$\mathbb{x}$
and the xlabel appear like this
https://i.stack.imgur.com/NCI4n.png
please how I can fix this problem.
The problem with your example is the mathbb command, which is not in base Latex. To show this, replace the \mathbb with, e.g., \mathrm. This will work.
I've never seen an example of adding Latex packages into the Matlab environment. (Update below)
You can have a look at this question, which includes a very aggressive potential way to add the package you need. But it's not clear to me if the solution proposed is actually functional.
How do you use the LaTeX blackboard font in MATLAB?

Endash for minus sign instead of hyphen for matlab

So here's my code:
set(groot, 'defaultAxesTickLabelInterpreter', 'latex') %For axes;
ax = gca;
yticklabels(ax, strrep(yticklabels(ax),'--','–'));
set(ax,'ticklabelinterpreter','tex') %or 'tex' but not 'latex'
figure(1)
t= [0:0.01:2*pi];
x = sin(t);
y = cos(t)
plot(t, x, t, y)
Output:
I tried the solution here, but the hyphens still remain there. I want the en-dash to appear because it's the standard sign for the negative sign. What is the correct way of getting an en-dash to appear instead of the hyphen?
This post at MATLAB Answers explains how to set the (default) interpreter for the axes' labels.
set(groot,'defaultAxesTickLabelInterpreter','latex');
You need to call this before plotting.
Having this set, the tick-labels will be interpreted as LaTeX code. Here is a comparison. The last two examples includes #XiangruiLi's answer (the next code snippets must be called after the plot was created):
yticklabels(gca, strrep(yticklabels(gca),'-','--'));
yticklabels(gca, strrep(yticklabels(gca),'-','$-$'));
none:
latex:
latex + strrep(...,'-','--')):
latex + strrep(...,'-','$-$')):
While the last is probably what you wanted, note that this is certainly not the representation MATLAB intended. It is therefore the question if you really need/want to go through this fuzz.
It seems to me you mis-used strrep. This worked for me:
yticklabels(ax, strrep(yticklabels(ax),'-','--'));
Using the actual Unicode minus character should also work (also in Octave):
yticklabels(gca, strrep(yticklabels(gca),'-','−'));
In this case, there is no need to set the interpreter to LaTeX.

Why using _LatexSmallFractionConstant:=1 causes bad latex to be generated?

The option _LatexSmallFractionConstant:=1 is very useful in Maple, as it makes the fraction generated in latex looks natural. Here is an example
restart;
latex(1/2);
1/2
Now using _LatexSmallFractionConstant:=1, the output becomes
restart;
_LatexSmallFractionConstant:=1:
latex(1/2);
{\frac{1}{2}}
Which is the correct Latex.
But _LatexSmallFractionConstant:=1 causes big problem in other places, like here
mu:=1/((4*t+1)^(8/5)*(t-1)^(7/5));
Now see what happens when latex is generated with _LatexSmallFractionConstant:=1 set
_LatexSmallFractionConstant:=1:
latex(simplify(mu))
{1 \left( 4\,t+1 \right) ^{-{\frac{8}{5}}} \left( t-1 \right) ^{-{
\frac{7}{5}}}}
Which is completely wrong. It renders as
Removing _LatexSmallFractionConstant:=1: gives
restart;
mu:=1/((4*t+1)^(8/5)*(t-1)^(7/5));
latex(simplify(mu))
{\frac {1}{ \left( 4\,t+1 \right) ^{8/5} \left( t-1 \right) ^{7/5}}}
Which renders correctly.
Is there a way to correct the above, so I can still use _LatexSmallFractionConstant:=1: ? I need to use this option to make fractions appear in correct Latex, but I also do not want to obtain bad Latex in other places like the above.
Is there a solution to this problem?
Maple 2018.1 on windows.
It's a bug. You should report it. I'd link to where, but at the moment, it looks like both www.maplesoft.com and www.mapleprimes.com are down.

Indentation in MATLAB important?

I have written a code in MATLAB for university and I have been told that indentation is important and it caused me to lose 3 marks out of 8 for it. Is indentation not just "style"?
The code if as follows:
function[pp,pb,pd]=demopoly(fname,N)
%The function reads the data from the file and computes the coefficients of a polynomial of degree N of three polynomials pp, pb, and pd that best fit the population, births and deaths in the data.
%The function returns the three variables pp, pb and pd in this order.
C=csvread(fname);
pp=polyfit(C(:,1),C(:,2),N);
pb=polyfit(C(:,1),C(:,3),N);
pd=polyfit(C(:,1),C(:,4),N);
end
Can someone let me know where the indentations should have occurred?
Indentation is just Ctrl+A and Ctrl+I away. Seems difficult? Other answers have pointed out why indentation is important. Regarding the question where indentation should have occured, the fact is there's no indentation problem in the code that you showed.
If I had to cut your marks for the code you showed, I'll cut for:
Having no line breaks in that first comment. It's too long and requires scrolling to read.
Extra line breaks in code.
Although Matlab code is not sensitive to indendation (unlike, for example, Python), it's very important to use a indentation in order to improve the code readability and it's maintainability.
If you are too lazy to manually indent your code while writing it down, Matlab offers you a Smart Indent function that you can apply to your files once they are completed (more info here). If you are too lazy even for a CTRL+A CTRL+I, you could write a small "batch script of madness" that applies the Smart Indent to all the .m files located within a specific folder:
files = dir(fullfile(folder,'*.m'));
for i = 1:numel(files)
file_name = files(i).name;
file_path = fullfile(folder,file_name);
file_handle = matlab.desktop.editor.openDocument(file_path);
file_handle.smartIndentContents()
file_handle.save()
file_handle.close()
end
This is how I would format (and optimize a little bit) your function:
% The function reads the data from the file and computes
% the coefficients of a polynomial of degree N of three
% polynomials (pp, pb, and pd) that best fit the population,
% births and deaths in the data.
% The function returns the three variables pp, pb and pd
% in this order.
function [pp,pb,pd] = demopoly(fname,N)
C = csvread(fname);
C_1 = C(:,1);
pp = polyfit(C_1,C(:,2),N);
pb = polyfit(C_1,C(:,3),N);
pd = polyfit(C_1,C(:,4),N);
end

Evaluating a function in matlab

I have expanded sin function into a Taylor series. Now I want to evaluate it at specific point. I get strange "MuPAD" errors in matlab. What am I doing wrong?
function r1=taylor_sine
syms x;
mysine = taylor(sin(x), 63, 0);
r1 = funm(220, mysine);
Did you really mean 220? Or did you mean 220 degrees in which case you should use 220*pi/180?
I think it should be subs not funm
r1 = double( subs(mysine, x, 220*pi/180) );
I'm not 100% familiar with the syntax you're using, maybe the inline function syntax is slightly different from the expanded syntax, however it looks like your function doesn't have a cleary defined input and output. A non-inline matlab function should look like this:
%Comment
function [ theta ] = FunctionName( alpha, beta )
theta = alpha + beta;
end
Try to create your function in a separate .m file (filename the same as the function name). After you've created the .m file make sure that it's located in the search path of MatLab (check if the autocompleter shows your function name when you type it in partially).
As for the actual body of your function, I see a few weird things. What is "syms x" supposed to do? I would replace this line with "x = -pi:0.001:pi;" (have x be a vector from -pi to pi with increments of 0.001). or something analogue to that.
Also for normal Taylor aproximation I would use taylor(sin(x), 63) (the overload with 'v'- does a Maclaurin approximation). Also I wouldn't do a Taylor approximation up to the 63-1th order, that's way too high, maybe MatLab crashes on that.
In the following picture you can see that the 7th order approximation is already extremely good between -pi and pi.