Finite difference scheme and iteration what is wrong with my code? Please help me
Show older comments
function Perturbation(x,N,epsilon)
clc
close all
x=10;
N=10;
M=10;
h=1/M;
alpha=1;
beta=-1;
epsilon=0.01;
dtau=1/N; % step size
tau = 0:dtau:4; % Calculates upto y(3)
w = sym(zeros(1,length(tau)));
t= sym(zeros(1,length(tau))); % initial condition
F_0 = @(r) r; % change the function as you desire
F_10 = @(r) 2*r;
F_11 = @(r) 3*r;
for i=1:(length(h)-1) % calculation loop
tk=i*tau;
t=tk;
w(0) = F_0(t(i));
w(1) = F_10(t(i))+F_11(t(i));
w(i+1) = w(i) + w(i-1)+x % main equation
end
4 Comments
Asvin Kumar
on 4 Jan 2021
It's unclear what you're trying to do or where you're stuck or whether you're encountering an error.
Please provide more details on the expected output/behaviour so the community may help you.
Just at first glance, I see that h is a scalar but you use it to determine values of i. This would never iterate. Maybe you intended to use length(t) instead? You're redefining t (or tk) inside the for loop but you don't use it. w(0) and w(1) needn't be computed repeatedly. Your function arguments are overwritten.
Looks like you're new to MATLAB. Please considering taking the MATLAB onramp training which will help you get started with the language.
MATLAB Onramp: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
OZGUR YILDIRIM
on 5 Jan 2021
Asvin Kumar
on 5 Jan 2021
https://www.mathworks.com/help/symbolic/subs.html Should do just that.
OZGUR YILDIRIM
on 5 Jan 2021
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!