Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I wanna solve this form of equation:
x' = -A.x + B.|sin(100*pi*t)|
and i use ode45 like this:
function find_x
t = 0:0.001:10;
x0 = 0;
R1 = 90000;
R2 = 1000;
C = 0.001;
[t,x]=ode45(#rhs, t , x0);
plot(t,x);
function dxdt = rhs(t,x)
dxdt = -(C/R1 + C/R2)*x + C/R1*abs(sin(100*pi*t)) ;
%It's form is dx/dt = -A.x + B.U(t)
end
end
but i think it give me the wrong answer.
actually, i get this equation from a problem "find output voltage form after a diode bridge and a capacitor" like this:
can anyone suggest to me a another way to solve it ? thanks.
Not really the place to solve this, and this takes me back to my signal & systems days, but basically your equations should have C as a divider...
Remember it's
I = C *dV/dt
Therefore if you have dV/dt on the RHS, you should expect to see 1/C on the LHS:
function khan
t = 0:0.001:10;
x0 = 0;
R1 = 90000;
R2 = 1000;
C = 0.001;
options = odeset('RelTol',1e-6,'AbsTol',1e-8);
[t,x]=ode45(#rhs, t , x0,options);
plot(t,x);
function dxdt = rhs(t,x)
dxdt = -(1/R1 + 1/R2)*x/C + 1/R1*abs(sin(100*pi*t))/C ;
%It's form is dx/dt = -A.x + B.U(t)
end
end
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So I have the following matlab code where I am attempting to calculate Newton's Method to solve a non-linear equation (in particular, identify the square root). I am unable to see the output of my two fprintf() statements, so I think I fundamentally misunderstand MATLAB's way of calling functions.
I feel that there is something so fundamentally basic that I missing here.
I am running this over the Matlab web application https://matlab.mathworks.com/
My code is the following:
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = # (x) x^2 - p;
egFunDer = # (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end
%%
function answer = main
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
end
I expect to see a value for answer, which should print to the console, as well as the two fprintf() statements I threw in for debugging.
You need to call NewtonRoot function in main code.
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = # (x) x^2 - p;
egFunDer = # (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%%
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
For instance: a, b, c and n are the three constants, which are required to be calculated by using data fitting method in a particular equation.
How can I calculate the statistics (mean, standard deviation, variance, skewness value and student t-test value) of the parameters as of a custom equation, for example the quadratic plateau equation?
Example:
x=[0,40,80,100,120,150,170,200],
y=[1865,2855,3608,4057,4343,4389,4415,4478]
y=a*(x+n)^2+b*(x+n)+c, x < xc(Ymax) ....(1) y=yp, x >= xc(Ymax) ....(2)
I have fitted this equation by given code:
yf = #(b,x) b(1).*(x+n).^2+b(2)*(x+n)+b(3); B0 = [0.006; 21; 1878];
[Bm,normresm] = fminsearch(#(b) norm(y - yf(b,x)), B0); a=Bm(1);
b=Bm(2); c=Bm(3); xc=(-b/(2*a))-n; p=p=a*(xc+n)^2+b*(xc+n)+c;
if (x < xc)
yfit = a.*(x+n).^2+ b*(x+n)+c;
else
yfit = p;
end
plot(x,yfit,'*')
hold on; plot(x,y); hold off
Note: I have already used the polyfit command, it was helpful and provided me the results. However, I really don’t find it suitable, as there is no option to customize the equation. Can I find these statistics by any code?
Questions 1, 2 and 4)
Good practice is to set initial values close to the final result if you have previous knowledge about the equation system:
What you have is an overdetermined system of linear equations.
y(1) = a*x(1)^2 + b*x(1) + c
y(2) = a*x(2)^2 + b*x(2) + c
y(3) = a*x(3)^2 + b*x(3) + c
…
y(n) = a*x(n)^2 + b*x(n) + c
or in general:
y = A*X, where
A = [a; b; c]
X = [x(1)^2 x(1) 1;
x(2)^2 x(2) 1;
x(3)^2 x(2) 1;
...
x(n)^2 x(n) 1]
One of the common practices to fit the overdetermined system (since it has no solution) is "least square fit" (mldivide,\ (link))
x=[0; 40; 80; 100; 120; 150; 170; 200];
y=[1865; 2855; 3608; 4057; 4343; 4389; 4415; 4478];
X = [x.^2 x ones(numel(x),1)];
A = y\X;
a0=A(1); %- initial value for a
b0=A(2); %- initial value for b
c0=A(3); %- initial value for c
You can customize equation, when you customize your X and A
but you also can set initial values to ones, it should have neglectable small impact on the result. More related to Question 4
a0=1;
b0=1;
c0=1;
or to random values
rng(10);
A = rand(3,1);
a0=A(1);
b0=A(2);
c0=A(3);
Question 3 - Statistics
If you need more control on monitoring of optimization process, use more general form of writing anonymous function (in code below> myfun) to save all intermediate values of parameters (a_iter, b_iter, c_iter)
function Fiting_ex()
global a_iter b_iter c_iter
a_iter = 0;
b_iter = 0;
c_iter = 0;
x=[0; 40; 80; 100; 120; 150; 170; 200];
y=[1865; 2855; 3608; 4057; 4343; 4389; 4415; 4478];
X = [x.^2 x ones(numel(x),1)];
A = y\X;
a0=A(1);
b0=A(2);
c0=A(3);
B0 = [a0; b0; c0];
[Bm,normresm] = fminsearch(#(b) myfun(b,x,y),B0);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
plot(x,yfit,'*')
hold on;
plot(x,y);
hold off
% Statistic on optimization process
a_mean = mean(a_iter(2:end)); % mean value
a_var = var(a_iter(2:end)); % variance
a_std = std(a_iter(2:end)); % standard deviation
function f = myfun(Bm, x, y)
global a_iter b_iter c_iter
a_iter = [a_iter Bm(1)];
b_iter = [b_iter Bm(2)];
c_iter = [c_iter Bm(3)];
yf = Bm(1)*(x).^2+Bm(2)*(x)+Bm(3);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
f = norm(y - yfit);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am supposed to write a script that provides multiple lines of projectile motion but my code doesn't seem to give me what I need.
disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
v0 = input('Please enter the initial speed');
x0 = 0;
y0 = 0;
g = 9.81;%m/s^2
T = 5 : 5 : 85;
vx = v0*cosd(T);
vy = v0*sind(T);
t = (2*v0.*sind(T))/g;
y = y0 + (vy.*t) - ((g.*(t.^2))/2);
x = x0 + vx.*t;
plot(x,y)
This is how the graph should look like:
In your code, T represents the initial degree. You want to calculate x and y for different initial degrees (5:5:85). Use a for loop for T and plot x and y for different t.
disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
v0 = input('Please enter the initial speed');
x0 = 0;
y0 = 0;
g = 9.81;%m/s^2
for T = 5 : 5 : 85
vx = v0*cosd(T);
vy = v0*sind(T);
t = linspace(0,(2*v0.*sind(T))/g,100);
y = y0 + (vy.*t) - ((g.*(t.^2))/2);
x = x0 + vx.*t;
plot(x,y)
hold on
xlim([-inf inf])
ylim([-inf inf])
end
Output:
This program will calculate the trajectory of a ball thrown at an initial speed vo \n
Please enter the initial speed10
This question already has answers here:
Newton-Raphson Method in Matlab
(2 answers)
Closed 6 years ago.
If I have a code like this
funk=#(x) x2-4;
dfunk=#(x) 2*x;
xk=4;
for i = 1:5
xk+i= xk-funk(of what? xk or #(x) x2-4) / dfunk( same problem here);
end
I do not know what am I supposed to write in brackets it is supposed to be Newton Raphson method.
Thanks for help
You should not add i to your xk in the for loop. As Newton's Metod
generates a series which converges to a root of the funktion the +i only operates on the indices of the series.
Your code should look like this.
funk = #(x) x^2-4;
dfunk = #(x) 2*x;
xk = 4;
for i=1:5
xk = xk - funk(xk)/dfunk(xk)
end
Anyway I would not recommend to use a fixed condition like i=1:5 but something like
TOL = 10e-4;
DIFF = 1; % something larger than TOL
funk = #(x) x^2-4;
dfunk = #(x) 2*x;
xk = 4;
while (DIFF > TOL)
aux = xk - funk(xk)/dfunk(xk);
DIFF = abs(xk-aux);
xk = aux;
end
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have N-time steps and want to create N real numbers corresponding to each time step as follows: the numbers should be in the range [a, b] with a switching probability of p.
Ie, if kth number (corresponding to kth time step) is n, then the probability that k+1th number (corresponding to k+1th time step) is any other number different than n is p. All created numbers should be in the range [a,b].
How can this be done in matlab?
Thanks.
I am not really sure that I got all of your requirements but this might be the script you are searching for:
N = 10; % count of numbers
p = 0.2; % switching probability
a = 5;
b = 20;
% init empty numbers and get the first random number
numbers = zeros(N,1);
numbers(1) = rand(1) * (b-a) + a;
for iNumber = 2:N
% test if number must change
if rand(1) < (1-p)
% the number must be the same than the previous
% copy the value and go to next number
numbers(iNumber) = numbers(iNumber-1);
continue;
else
% a new number must be found
while 1
% get a new random number
numbers(iNumber) = rand(1) * (b-a) + a;
% check if the new random number is different from the previous
if numbers(iNumber) ~= numbers(iNumber-1)
% in case they are different, the while 1 can be stopped
break;
end
end % while 1
end % if rand(1) < (1-p)
end % for iNumber = 2:N
Guaranteed super random:
N = randi(100);
p = rand;
l1 = rand*randi(100);
l2 = rand*randi(100);
if l2 < l1
[l2 l1] = deal(l1,l2);
end
out = []
while isempty(out)
if rand>rand
n = 2:N
a = rand([N,1])*(l2-l1)+l1;
x = rand([N-1,1])<p;
n(x==0)=n(x==0)-1;
n = [1 n];
out = a(n);
end
end