How to plot this equation in matlab - matlab

alright well I have the follow function
y=sin(x)^2 + [(10+2x+x^2) / (5+2x^2)]
i need to plot it on the interval y = -2 to y = 2 so how would I set that up?
I did this in matlab
>> y = sin(x).^2 + (10 + 2*x + x.^2)/(5+2*x.^2)
>> x = -2:0.01:2;
is that a correct setup? Or have I done something wrong

You need to declare a variable before you use it. In this case, x doesn't depend on y, so declare it first. In addition, there is a ./ operator missing.
x = -2:0.01:2;
y=sin(x).^2 + (10+2*x+x.^2) ./ (5+2*x.^2);
plot(x,y)

f = #(x) sin(x)^2 + [(10+2*x+x^2) / (5+2*x^2)];
ezplot(f)

Related

I need to solve this first-order system ODE using Matlab

I have to solve this first-order system ODEs using Matlab.
y' + y - z - u = 0.
z' - y + z - u = 0.
u' - y - z - u = 0.
y(0)=1, z(0)=0, u(0)= 0
The analytical solution of above system of first-order ODEs is:
y = (1/3)e^–x + (1/2)e^–2x + (1/6)e^2x
z = (1/3)e^–x – (1/2)e^–2x + (1/6)e^2x
u = (1/3)e^2x – (1/3)e^–x
I have this code to solve it and plot the numerical solution I obtain using the ODE solvers and the analytical solution I have in the statement.
----------
clear
%options= odeset('RelTol',1e-5);
options= odeset('RelTol',1e-5,'AbsTol',1e-7);
[t23,y23]= ode23('functionB',[0 1],[1 0 0],options);
[t23s,y23s]= ode23s('functionB',[0 1],[1 0 0],options);
figure
ya =((1/3)*exp(-t23) + (1/2)*exp(-2*t23) + (1/6)*exp(2*t23));
za =((1/3)*exp(-t23) - (1/2)*exp(-2*t23) + (1/6)*exp(2*t23));
ua =((1/3)*exp(2*t23) + (1/3)*exp(-t23));
plot(t23,ya,t23,za,t23,ua);
title('\bf{Analytical solutions }')
figure
plot(t23,y23(:,1),'m-.',t23,y23(:,2),'g:', t23,y23(:,3),r);
title('\bf{Numerical solutions using} \it{ode23}')
figure
plot(t23s,y23s(:,1),'m-.',t23s,y23s(:,2),'g:', t23s,y23s(:,3),r);
title('\bf{Numerical solutions using} \it{ode23s}')
%legend('ya','ode23','ode23s',0)
%text(3.4,-1.7,'ya = -2sin(t) - cos(2t) ')
%title('\bf{Analytical and numerical solutions using} \it{ode23, ode23s}')
----------
And also I have the function functionB:
-------
function dy= functionB(t,y)
%-y+z+u
%y-z+u
%y+z+u
dy =[??????];
-------
I don't know how to write the solutions in the code of the function to use it in my code.
If someone can help me with this I will be so grateful.
Thanks in advance.
The MATLAB documentation has some good examples about this.
You must rearrange your ODE like this:
y' = -y + z + u = 0
z' = y + z + u = 0
u' = y + z + u = 0
You then substitude y by y(1), z by y(2) and u by y(3) and now you can write your function like this:
function dy = functionB(t,y)
dy = zeros(3,1);
dy(1) = -y(1)+y(2)+y(3);
dy(2) = y(1)-y(2)+y(3);
dy(3) = y(1)+y(2)+y(3);
end

Solving systems of equations, receiving z and z1 in solution?

I passed the following nonlinear system to Matlab:
2(x−p1)+2(xy−p3)y = 0
2(y−p2)+2(xy−p3)x = 0
and used syms to find solution for x and y symbolically but I got:
sol.x
ans =
(p1^3 + p3*p1^2*z1 + p1*z1^4 - 1.0*p2*p1*z1^3 + p1*z1^2 - 1.0*p2*p1*z1 + p3*z1^3 - 1.0*p2*p3*z1^2 + p3*z1 - 1.0*p2*p3)/(p1^2 + p3^2)
and
sol.y
ans =
z1
where z1 = RootOf(z^5 - p2*z^4 + 2*z^3 - z^2*(2*p2 - p1*p3) + z*(p1^2 - p3^2 + 1) - p1*p3 - p2, z)
I dont understand where z come from? what is z?
Your y solution is expressed in terms of roots of a polynomial in z that depends on your equation's parameters also.
To show why is difficult to answer your question in the present form, please allow me to rephrase it: the numbers I'm looking for are the roots of an equation f(z) = 0; now, where z comes from? :-)

plot a nonlinear function matlab

I'm very new to Matlab and have problem plotting this nonlinear 2D function graph using Matlab.
a lot of errors generated after the below is run.
fun1 = 20 + 10 + 15;
fun2 = 20 + (x * 0.00125 ) + 15;
fun3 = (x * 0.0025) + 15;
fplot(fun1,[0 8000])
fplot(fun2,[8000 16000])
fplot(fun2,[16000 positive infinity])
I appreciate a lot to your efforts and kindness for replying my question
Best Regards
Your first three expressions do not define functions. Please read the documentation about the correct syntax.
fun1 = #(x)(20 + 10 + 15);
First create a file fun.m which contains your function definition
function y = fun(x)
if x < 8000
y = 20 + 10 + 15;
elseif x < 16000
y = 20 + (x * 0.00125) + 15;
else
y = x * 0.0025 + 15;
end
end
Then you can plot it with
fplot(#fun, [0 24000])
which results in
If you do some reading in fplot you will find out that
for fplot(fun,limits)
fun must be
The name of a function
A string with variable x that may be passed to eval, such as 'sin(x)', 'diric(x,10)', or '[sin(x),cos(x)]'
A function handle
so in your case you need to change all of you fun to strings just add ' before and after the expression
as for the last line change it to be
fplot(fun2,[16000 inf])
although i don't think this would give you any good results

Matlab - publish file in PDF-format

I have a question regarding Matlab's option to publish in PDF. Say I have the following code:
%1D functions and plotting
%1. We calculate y given the following function: y(x) = 2x^2 +
%3x + 1, for x = 10.
x = 10;
y = 2*x.^2 + 3*x + 1
%2. We calculate y given the following function: y(x) = ax^2 + bx + c, with a
%=2, b=3, c=0 and x = 100.
a=2;
b=3;
c=0;
x=100;
y = a*x.^2 + b*x + c
. . . more code follows here
When I choose to publish this as PDF the answers to problems 1 and 2 (where I calculate two different values for the variable 'y') do not appear at the line where I calculate the value (where I write y = 2*x.^2 + 3*x + 1 for instance). Instead, the values of the 'y' variables appear at the end of the document where it says 'y = 231' and 'y = 20300'. Is there any way I can get this to be included right after I define the variable without separating the document into cells? Or is this a default thing that I can not do anything about? I would really appreciate any input!
The general approach (indipendent of the output format) is to restart the paragraph with line break and %%:
%% 1. We calculate y given the following function: y(x) = 2x^2 +
% 3x + 1, for x = 10.
x = 10;
y = 2*x.^2 + 3*x + 1
%% 2. We calculate y given the following function: y(x) = ax^2 + bx + c, with a
% =2, b=3, c=0 and x = 100.
a=2;
b=3;
c=0;
x=100;
y = a*x.^2 + b*x + c

Matlab Differentiation

I need to write a for loop in matlab to solve a derivative using the forward difference method. The function to derive is 10+15x+20x^2 from 0 to 10 using steps of 0.25. I have tried using
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
y(1) = 45; size(x)
for i=2:47,
y(i) = y(i-1) + h*(15+40*x);
end
I'd do like this, as a start,
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
diff(y)./diff(x)
or, as alternative,
syms x;
y = 20.*x.^2 + 15.*x + 10;
dy = diff(y,1);
h=.25;
xx=[0:h:10];
res = subs(dy,xx);