I have to make a 2D graph in Scilab, and I'm not sure if the code I wrote is correct.
This problem aims for 2-D plot.
Plot the function y = x2 − 2 in the interval [-5, 5]
I have to graph this equation in Scilab and I'm not sure if the way y wrote it is correct or not.
--> x = -5 : 5;
--> y = x^2 - 2
Warning: Syntax "vector ^ scalar" is obsolete. It will be removed in Scilab 6.0. Use "vector .^ scalar" instead.
y =
23. 14. 7. 2. - 1. - 2. - 1. 2. 7. 14. 23.
--> plot(x,y)
The only weak point in your code is
y=x^2-2 instead of y=x.^2-2
Related
I've been assigned a project that requires me to plot some quadratic surfaces. I tried to be diligent and download some software so that my graphs look better than those done with other free online resources. I decided to try Octave and see if I can make it work but I've ran into a problem. When trying to plot:
I've checked some tutorials but so far I haven't been able to pinpoint my error. This is the code I was using:
clear;
x = [-3:1:3];
y = x;
[xx,yy] = meshgrid(x,y);
zz=sqrt(-9*xx.^2+9*yy.^2);
figure
mesh(xx,yy,zz);
Any suggestions are appreciated.
The error thrown to the command window for your script is:
error: mesh: X, Y, Z, C arguments must be real
error: called from
mesh at line 61 column 5
blah at line 15 column 1
Since you x and y are real, the imaginaries are coming from a square-root of a number less than 0. Looking at your equation, this will happen for any (x, y) pair where x is greater than y.
The easiest fix is to set all complex numbers (values of zz with a non-zero imaginary part) to 0 (which will plot the value) or NaN (which will not plot the value. Consider this script (yours plus filtering):
clear;
x = -3:0.1:3;
y = x;
[xx,yy] = meshgrid(x,y);
zz=sqrt(-9*xx.^2+9*yy.^2);
figure
% Set all zz with nonzero imaginary part to NaN
zz(imag(zz)~=0) = NaN;
% % Set all zz with nonzero imaginary part to 0
% zz(imag(zz)~=0) = 0;
mesh(xx,yy,zz);
I would prefer this:
x = -3:0.1:3;
y = x;
[xx,yy] = meshgrid(x,y);
zz=sqrt(-9*xx.^2+9*yy.^2); % zz will have both + and -
figure
% zz = abs(zz) ;
mesh(xx,yy,abs(zz));
hold on
mesh(xx,yy,-abs(zz));
I have an equation that I created on Desmos website
I used the code below to try and recreate it in Octave. But when I plot it, it comes out different. How can I fix the code in Octave (without changing the main equation, if possible) so it looks like the Desmos image?
x = linspace(0,1,20);
y = linspace(0,1,20);
S=[13.2];
T=1.12;
for zz=1:1:length(S)
eq1=exp(S(1,zz)*T*log(x))+exp(S(1,zz)*T*log(y));
hold on
plot(x,eq1)
zz
end
PS: I'm using Octave 4.2.2
S = 13.2;
T = 1.12;
f = #(x)exp(log(1-exp(S*T*log(x)))./(S*T));
fplot(f, [0, 1])
Desmos.com does not plot (x,eq1) but (x,y) with the constraint that x, y satisfy the given equation. So, you solve for y for each value of x, and plot the pairs (x,y).
Since log(x), log(y) exist, x and y are >0 (otherwise you would have to plot for x<0 as well).
clear; clc;
x = linspace(0,1,150);
S = 13.2;
T = 1.12;
y = zeros(size(x));
for i = 1:length(x)
y(i) = (1-exp(S*T*log(x(i))))^(1/S/T);
end
plot(x,y)
Notes:
1) I assume by log(x) you mean ln(x) (logarithm using base e).
2) I used a more dense discretization with 150 points so that the plotted curve appears smoother.
3) Mathematically, linspace(0,1,150) should not work, as log(x=0) is not defined. However for Matlab log(0) = -inf which means that exp(-inf) = 0. That's why no runtime error is thrown.
4) By the way, the provided equation can be simplified to x^(ST) + y^(ST) = 1, with the constraints that x, y > 0.
I have two points which form one line: (1,4) and (3,6), and another two which form another line: (2,1) and (4,2). These lines are continuous and I can find their intersection points by finding the equation for each line, and then equating them to find the x value at the intersection point, and then the y value.
i.e. for the first line, the equation is y = x + 3, and the second is y = 0.5x. At the intersection the y values are the same so x + 3 = 0.5x. So x = -6. Subbing this back into either of the equations gives a y value of -3.
From those steps, I now know that the intersection point is (-6,-3). The problem is I need to do the same steps in Excel, preferably as one formula. Can anyone give me some advice on how I would start this?
Its long but here it is:
Define x1,y1 and x2,y2 for the 1st line and x3,y3 and x4,y4 for the second.
x = (x2y1-x1y2)(x4-x3)-(x4y3-x3y4)(x2-x1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
y = (x2y1-x1y2)(y4-y3)-(x4y3-x3y4)(y2-y1) / [ (x2-x1)(y4-y3) - (x4-x3)(y2-y1) ]
Note that the denominators are the same. They will be ZERO! when the system has no solution. So you may want to check that in another cell and conditionally compute the answer.
Essentially, this formula is derived by solving a system of equations for x and y by hand using generic points (x1,y1), (x2,y2), (x3,y3), and (x4,y4). Easier yet, is solving the system by hand using well developed linear algebra concepts.
Wikipedia outlines this procedure well: Line-line intersection.
Also, this website describes all the different formulas and lets you put in whatever data you have in any mixed format and provides many details of the solutions: Everything about 2 lines.
Here's a matrix based solution:
x - y = -3
0.5*x - y = 0
Written as a matrix equation (I apologize for the poor typesetting):
| 1.0 -1.0 |{ x } { -3 }
| 0.5 -1.0 |{ y } = { 0 }
You can invert this matrix or use LU decomposition to solve it to get the answer. That method will work for any number of cases where you have one equation for each unknown.
This is easy to do by hand:
Subtract the second equation from the first: 0.5*x = -3
Divide both sides by 0.5: x = -6
Substitute this result into the other equation: y = 0.5*x = -3
can you help me, I 'm trying to plot the function y=1/x in scilab,
the graph that throws me is incorrect
x = [1:1:10]';
y = 1./x;
plot(x,y)
and throws me these results
y=
0.0025974
0.0051948
0.0077922
0.0103896
0.0129870
0.0155844
0.0181818
0.0207792
0.0233766
0.0259740
and this result is wrong , as would be the code ,
Thanks for the help :)
Write
y = 1 ./ x;
instead of
y = 1./x;
From the documentation (emphasis is mine):
a ./ b is the matrix with entries a(i,j)/ b(i,j). If b is scalar (1x1 matrix) this operation is the same as a./b*ones(a). (Same convention if a is a scalar).
Remark that 123./b is interpreted as (123.)/b. In this cases dot is part of the number not of the operator.
I am using this octave code for solving differential equation.
# Define the right-hand side of the equation:
xvall= -11 ;#xvall
xvalu= 10 ;#xvalu
range=5000;
function ret=f(x,t);ret= t ;end;
# ywill be the values of the function at these moments of time.
t=linspace(xvall,xvalu,range);
y=lsode ('f', 2, linspace(xvall,xvalu,range));
y
plot(t,y);
i got the graph like this .
But when the same conditions are passed to wolfram alpha
I am getting the graph from 60 to 0 for y value
graph is
why is the graph behaving differently in two situations.
https://www.wolframalpha.com/input/?i=Runge-Kutta+method%2C+dy%2Fdx+%3D+x%2C+y%280%29+%3D+2%2C+from+-11+to+10%2C+h+%3D+0.25
To specify an initial value problem for an ordinary differential equation you need to define the initial condition. Here for Octave you have specified x(-11) = 2 since xvall = -11 and for Wolfram Alpha you have specified y(0) = 2. That is why you have two different solutions.
Octave
Octave's lsode (f,x_0,ts) solves the following initial value problem
dx/dt = t
x(t_0) = x_0
t in ts
Here ts is specified as a set of points in the interval [t_0,t_1]. You have specified t_0 = -11, t_1 = 10.
In closed form the solution to this problem is x = (t^2 - 117) / 2
Wolfram
For Wolfram you have used the semi-formal syntax:
Runge-Kutta method, dy/dx = x, y(0) = 2, from -11 to 10, h = 0.25
In closed form the solution to this problem would be y = (x^2 + 4) /2
The corresponding initial value problem is clearly different. Hence different results.