Empty plot is showing because of my er

6 ビュー (過去 30 日間)
Panda05
Panda05 2023 年 11 月 30 日
コメント済み: Panda05 2023 年 11 月 30 日
Hello guys , is there a way i can store my er as a vector that i can later on use to plot with N ? thank you for your suggestions. The code is shown below. I just want to be able to use er in the for loop to plot with N , how can i retrieve the er values into a vector
N=[10,20,40,80];
for i = 1:length(N)
x = (0:N(i))./N(i);
xsamples = 0:0.005:1;
y = f(xsamples);
fxn = Lagpoly(xsamples,x,y,N);
er= max(abs(fxn-y));
end
plot(N, er, '*-', 'DisplayName', 'Data Points');
%plot(N,N);
%set(gca, 'XScale', 'log')
%set(gca, 'YScale', 'log')
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 30 日
編集済み: Dyuman Joshi 2023 年 11 月 30 日
Even if the er is a scalar, the code should plot something, see below.
If you get an empty plot, your data could have NaN values.
Please attach the defition of the parameter Lagpoly, so that we can run the code and reproduce the error.
plot(1:10, 10, '*-')

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2023 年 11 月 30 日
Index er. This works:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
N = [10, 20, 40, 80];
for k = 1:length(N)
x = (0:N(k))./N(k);
xsamples = 0:0.005:1;
y = f(xsamples);
% fxn = Lagpoly(xsamples,x,y,N);
fxn = rand;
er(k) = max(abs(fxn-y));
end
% Plot on a log-log scale. Or use plot() for a linear scale.
loglog(N, er, '*-', 'DisplayName', 'Data Points',...
'LineWidth', 2, 'MarkerSize', 30);
grid on;
xlabel('N', 'FontSize', fontSize);
ylabel('er', 'FontSize', fontSize);
title('er vs. N', 'FontSize', fontSize);
%=======================================================================
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
Since you didn't provide Lagpoly I had to just use a random number to test it. Replace the rand line with your Lagpoly line of code.
To learn other fundamental concepts, invest 2 hours of your time here:
  1 件のコメント
Panda05
Panda05 2023 年 11 月 30 日
Thank you so much !!!!

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2023 年 11 月 30 日
編集済み: Torsten 2023 年 11 月 30 日
What x-y data are given to build the Lagrange Polynomial and in which x-data do you want to interpolate ? I assume that x-y are given arrays to build the Lagrange Interpolation upon and that you want to interpolate in xsamples. But your code takes xsamples and y to build the Lagrange polynomial and x for evaluation. I don't think this is what you want.
Further, you have to save "er" for each value of i.
Use
er(i)= max(abs(fxn-y));
instead of
er= max(abs(fxn-y));
  1 件のコメント
Panda05
Panda05 2023 年 11 月 30 日
Thank you so much !!!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSmoothing and Denoising についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by